基於第三方的微信授權登錄是之前筆者博文《QQ和新浪微博授權登錄》的姊妹篇,微信已經深入到每一個APP的縫隙,最常用的莫過分享和登錄了,接下來就以代碼的形式來展開微信登錄的相關說明,至於原理級別的oauth2.0認證體系請參考微信開放平台的相關說明和圖示 https://open.weixin.qq.com/
三,微信登錄授權開發:
1,到微信開發平台注冊相關APP,現在是等待審核成功後才能獲取到對應的key和secret;獲取成功後需要單獨申請開通登錄和支付接口,如圖

2,和QQ類似,需要填寫Url Schemes,如demo中的wxd930ea5d5a258f4f ,然後引入相應framework;
3,在AppDelegate中注冊和實現授權後的回調函數,代碼如下:
//向微信注冊
[WXApi registerApp:kWXAPP_ID withDescription:@weixin];
//授權後回調 WXApiDelegate
-(void)onResp:(BaseReq *)resp
{
/*
ErrCode ERR_OK = 0(用戶同意)
ERR_AUTH_DENIED = -4(用戶拒絕授權)
ERR_USER_CANCEL = -2(用戶取消)
code 用戶換取access_token的code,僅在ErrCode為0時有效
state 第三方程序發送時用來標識其請求的唯一性的標志,由第三方程序調用sendReq時傳入,由微信終端回傳,state字符串長度不能超過1K
lang 微信客戶端當前語言
country 微信用戶當前國家信息
*/
SendAuthResp *aresp = (SendAuthResp *)resp;
if (aresp.errCode== 0) {
NSString *code = aresp.code;
NSDictionary *dic = @{@code:code};
}
}
//和QQ,新浪並列回調句柄
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [TencentOAuth HandleOpenURL:url] ||
[WeiboSDK handleOpenURL:url delegate:self] ||
[WXApi handleOpenURL:url delegate:self];;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [TencentOAuth HandleOpenURL:url] ||
[WeiboSDK handleOpenURL:url delegate:self] ||
[WXApi handleOpenURL:url delegate:self];;
}
下面用代碼來實現:
第一步:code
- (IBAction)weixinLogin:(id)sender
{
[self sendAuthRequest];
}
-(void)sendAuthRequest
{
SendAuthReq* req =[[SendAuthReq alloc ] init];
req.scope = @snsapi_userinfo,snsapi_base;
req.state = @0744 ;
[WXApi sendReq:req];
}
這裡獲取後會調用之前在AppDelegate裡面的對應oauthResp回調,獲得得到的code。
第二步:token和openid
-(void)getAccess_token
{
//https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
NSString *url =[NSString stringWithFormat:@https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code,kWXAPP_ID,kWXAPP_SECRET,self.wxCode.text];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *zoneUrl = [NSURL URLWithString:url];
NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil];
NSData *data = [zoneStr dataUsingEncoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
if (data) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
/*
{
access_token = OezXcEiiBSKSxW0eoylIeJDUKD6z6dmr42JANLPjNN7Kaf3e4GZ2OncrCfiKnGWiusJMZwzQU8kXcnT1hNs_ykAFDfDEuNp6waj-bDdepEzooL_k1vb7EQzhP8plTbD0AgR8zCRi1It3eNS7yRyd5A;
expires_in = 7200;
openid = oyAaTjsDx7pl4Q42O3sDzDtA7gZs;
refresh_token = OezXcEiiBSKSxW0eoylIeJDUKD6z6dmr42JANLPjNN7Kaf3e4GZ2OncrCfiKnGWi2ZzH_XfVVxZbmha9oSFnKAhFsS0iyARkXCa7zPu4MqVRdwyb8J16V8cWw7oNIff0l-5F-4-GJwD8MopmjHXKiA;
scope = snsapi_userinfo,snsapi_base;
}
*/
self.access_token.text = [dic objectForKey:@access_token];
self.openid.text = [dic objectForKey:@openid];
}
});
});
}
利用GCD來獲取對應的token和openID.
第三步:userinfo
-(void)getUserInfo
{
// https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID
NSString *url =[NSString stringWithFormat:@https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@,self.access_token.text,self.openid.text];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *zoneUrl = [NSURL URLWithString:url];
NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil];
NSData *data = [zoneStr dataUsingEncoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
if (data) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
/*
{
city = Haidian;
country = CN;
headimgurl = http://wx.qlogo.cn/mmopen/FrdAUicrPIibcpGzxuD0kjfnvc2klwzQ62a1brlWq1sjNfWREia6W8Cf8kNCbErowsSUcGSIltXTqrhQgPEibYakpl5EokGMibMPU/0;
language = zh_CN;
nickname = xxx;
openid = oyAaTjsDx7pl4xxxxxxx;
privilege = (
);
province = Beijing;
sex = 1;
unionid = oyAaTjsxxxxxxQ42O3xxxxxxs;
}
*/
self.nickname.text = [dic objectForKey:@nickname];
self.wxHeadImg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dic objectForKey:@headimgurl]]]];
}
});
});
}
執行到這一步就算完成了整個授權登錄的功能,能把昵稱和頭像顯示出來,剩下的就是及時刷新你的token,詳情可參考開發文檔。
下面是登錄成功後的QQ,新浪微博,微信的真機運行成功截圖:

評價:微信的開發文檔相比容易理解和調試,雖然沒有demo,但是文檔比較詳細,所以可以在一定程度上減輕了開發的困難,但是相比之下微信的授權步驟比較麻煩,需要三步才能徹底獲取用戶信息,這點沒有QQ和新浪微博簡潔,需要有一定的閱讀和代碼功底,希望能給大家帶來幫助。
後記:微信授權登錄是對QQ和新浪微博登錄的姊妹篇,前兩者登錄代碼分析請參考上一篇博文,如需三種登錄方式的demo源代碼,該demo只需換掉默認宏定義和Url Schemes中默認的key即可正式使用,詳情請發Email:mmw05@163.com ,特別注意代碼不免費提供,需要支付一定金額,謝謝合作!