優(yōu)勢(shì):
通過(guò)微信授權(quán)來(lái)登錄APP眠屎,有以下優(yōu)勢(shì):
1.簡(jiǎn)化賬戶注冊(cè)過(guò)程剔交;
2.可以獲取到微信用戶的好友關(guān)系,來(lái)增強(qiáng)APP自身的社交功能改衩;
整體流程:
1.客戶端發(fā)送登錄請(qǐng)求至微信岖常;
2.用戶在微信上選擇是否同意授權(quán)登錄;
3.若是同意授權(quán)葫督,客戶端開(kāi)始向授權(quán)服務(wù)器獲取授權(quán)碼竭鞍;
4.授權(quán)碼獲取成功之后,通過(guò)授權(quán)碼向微信獲取用戶信息橄镜。
一偎快、請(qǐng)求認(rèn)證:
if (api.isWXAppInstalled())
{
final SendAuth.Req req =new SendAuth.Req();
req.scope ="snsapi_userinfo";
req.state ="wechat_sdk_demo_test";
api.sendReq(req);
} else {
showToast("請(qǐng)先安裝微信");
}
用這段代碼向微信開(kāi)放平臺(tái)請(qǐng)求授權(quán)碼code,可拉起微信并打開(kāi)授權(quán)登錄頁(yè)(前提是你安裝了微信應(yīng)用并已登錄,未登錄的會(huì)引導(dǎo)你先登錄)
二、用戶同意,返回code:
在你的包名相應(yīng)目錄下新建一個(gè)wxapi目錄紧唱,并在該wxapi目錄下新增一個(gè)WXEntryActivity類哥牍,該類繼承自Activity,
@Override
public void onResp(BaseResp resp)
{
int errorCode = resp.errCode;
switch (errorCode)
{
case BaseResp.ErrCode.ERR_OK:
//用戶同意煞抬,拿到code
String code = ((SendAuth.Resp) resp).code;
break;
case BaseResp.ErrCode.ERR_AUTH_DENIED:
//用戶拒絕
break;
case BaseResp.ErrCode.ERR_USER_CANCEL:
//用戶取消
break;
default:
break;
}
}
三泌类、利用code獲取access token:
private void getAccess_token(final String code)
{
String path = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
+ WxData.WEIXIN_APP_ID
+ "&secret="
+ WxData.APP_SECRET
+ "&code="
+ code
+ "&grant_type=authorization_code";
OkHttpUtils.get().url(path).build().execute(new StringCallback()
{
@Override
public void onError(okhttp3.Call call, Exception e) {
}
@Override
public void onResponse(String response)
{
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(response);
String openid = jsonObject.getString("openid").toString().trim();
// access_token就是授權(quán)碼
String access_token =
jsonObject.getString("access_token").toString().trim();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
WxData.WEIXIN_APP_ID和WxData.APP_SECRET就是你在微信開(kāi)放者平臺(tái)創(chuàng)建應(yīng)用時(shí)給你的缅刽。
四读跷、利用access token獲取用戶信息:
private void getUserInfo(final String access_token, final String openid)
{
String path = "https://api.weixin.qq.com/sns/userinfo?access_token="
+ access_token
+ "&openid="
+ openid;
OkHttpUtils.get().url(path).build().execute(new StringCallback()
{
@Override
public void onError(okhttp3.Call call, Exception e) {
}
@Override
public void onResponse(String response)
{
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(response);
String nickname = jsonObject.getString("nickname");
int sex = Integer.parseInt(jsonObject.get("sex").toString());
String headimgurl = jsonObject.getString("headimgurl");
String openid1 = jsonObject.getString("openid")梗搅;
imageUrl(headimgurl); // 顯示圖片
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
private void imageUrl(final String url)
{
new Thread(new Runnable()
{
@Override
public void run()
{
try {
URL url1 = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection)
url1.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(2000);
int responseCode = urlConnection.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = urlConnection.getInputStream();
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
// 這個(gè)post一般用來(lái)更新UI用,post將一個(gè)Runnable對(duì)象到 消息隊(duì)列
handler.post(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap); // UI顯示
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}