java获取钉钉用户access_token 完整代码
时间: 2023-10-09 09:07:35 浏览: 336
以下是获取钉钉用户access_token的完整Java代码示例:
```java
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.alibaba.fastjson.JSONObject;
public class DingTalkAccessToken {
public static void main(String[] args) {
String appKey = "your_app_key"; // 应用的AppKey
String appSecret = "your_app_secret"; // 应用的AppSecret
String accessToken = getAccessToken(appKey, appSecret);
System.out.println("Access token: " + accessToken);
}
public static String getAccessToken(String appKey, String appSecret) {
String accessToken = null;
String url = "https://oapi.dingtalk.com/gettoken?appkey=" + appKey + "&appsecret=" + appSecret;
try {
URL getUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
connection.setRequestMethod("GET");
connection.connect();
InputStream inputStream = connection.getInputStream();
byte[] data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
String result = new String(data, "UTF-8");
JSONObject jsonObject = JSONObject.parseObject(result);
accessToken = jsonObject.getString("access_token");
} catch (IOException e) {
e.printStackTrace();
}
return accessToken;
}
}
```
请将代码中的`your_app_key`和`your_app_secret`替换为您的应用AppKey和AppSecret。
阅读全文