109 lines
4.0 KiB
Java
109 lines
4.0 KiB
Java
package com.gh.gamecenter.douyinapi;
|
|
|
|
import android.content.Context;
|
|
import android.text.TextUtils;
|
|
|
|
import com.gh.common.constant.Config;
|
|
import com.lightgame.utils.Utils;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.InputStream;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
|
|
/**
|
|
* Created by khy on 30/06/17.
|
|
* 获取微信 accessToken 和 userInfo
|
|
*/
|
|
|
|
public class DouYinUserInfoThread extends Thread {
|
|
|
|
private Context mContext;
|
|
private OnUserInfoCallBackListener listener;
|
|
|
|
private String mCode;
|
|
|
|
//刷新access_token有效期
|
|
private String refresh_token = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN";
|
|
|
|
public DouYinUserInfoThread(OnUserInfoCallBackListener listener, String code, Context context) {
|
|
this.listener = listener;
|
|
this.mCode = code;
|
|
this.mContext = context;
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
super.run();
|
|
try {
|
|
String tokenUrl = "https://open.douyin.com/oauth/access_token?client_key=" +
|
|
Config.DOUYIN_CLIENTKEY + "&client_secret=" +
|
|
Config.DOUYIN_CLIENTSECRET + "&code=" +
|
|
mCode + "&grant_type=authorization_code";
|
|
String jsonResult = getJsonResultByUrlPath(tokenUrl);
|
|
JSONObject jsonObject = new JSONObject(jsonResult);
|
|
JSONObject data = jsonObject.optJSONObject("data");
|
|
|
|
String accessToken = data.optString("access_token");
|
|
String refreshToken = data.optString("refresh_token");
|
|
String openid = data.optString("open_id");
|
|
String unionid = data.optString("unionid");
|
|
long expiresIn = Long.parseLong(data.optString("expires_in"));
|
|
|
|
if (!TextUtils.isEmpty(accessToken) && !TextUtils.isEmpty(openid) && !TextUtils.isEmpty(refreshToken) && listener != null) {
|
|
// String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openid;
|
|
// String userInfo = getJsonResultByUrlPath(userInfoUrl);
|
|
JSONObject content = new JSONObject();
|
|
content.put("openid", openid);
|
|
content.put("access_token", accessToken);
|
|
content.put("access_token_expire", Utils.getTime(mContext) + expiresIn); // 有效期
|
|
content.put("refresh_token", refreshToken);
|
|
// content.put("refresh_token_expire", Utils.getTime(mContext) + 86400 * 30); // 有效期 30天
|
|
content.put("unionid", unionid);
|
|
|
|
listener.onComplete(content);
|
|
} else {
|
|
if (listener != null) {
|
|
listener.onError();
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
if (listener != null) {
|
|
listener.onError();
|
|
}
|
|
}
|
|
}
|
|
|
|
private String getJsonResultByUrlPath(String path) throws Exception {
|
|
URL url = new URL(path);
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
conn.setConnectTimeout(5000);
|
|
conn.setRequestMethod("GET");
|
|
conn.setDoInput(true);
|
|
int code = conn.getResponseCode();
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
byte[] buffer = new byte[1024];
|
|
int len;
|
|
Utils.log(DouYinUserInfoThread.class.getSimpleName(), "DouYinUserInfoThread-getJsonResultByUrlPath::" + code);
|
|
if (code == 200) {
|
|
InputStream is = conn.getInputStream();
|
|
while ((len = is.read(buffer)) != -1) {
|
|
baos.write(buffer, 0, len);
|
|
}
|
|
String str = new String(baos.toByteArray());
|
|
Utils.log(DouYinUserInfoThread.class.getSimpleName(), "DouYinUserInfoThread-Body::" + str);
|
|
return str;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public interface OnUserInfoCallBackListener {
|
|
void onComplete(JSONObject content);
|
|
|
|
void onError();
|
|
}
|
|
}
|