分別嵌入 新浪微博、QQ、微信 做第三方授權登入 獲取到頭像 暱稱等資訊
下面提到的這三種 授權登入 是分別嵌入,不是 share sdk 或者友盟 其它的。
一、下載sdk 地址
3.微信 微信SDK
二、程式碼編寫
怎麼嵌入 匯入庫,配置key 那些就不說。
在Applegate 裡面
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [WXApi handleOpenURL:url delegate:(id <WXApiDelegate>) self] | return [ WeiboSDK handleOpenURL:url delegate:(id <WeiboSDKDelegate>) self]|return [TencentOAuth HandleOpenURL:url]; }
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [ WeiboSDK handleOpenURL:url delegate:(id <WeiboSDKDelegate>) self ]|[WXApi handleOpenURL:url delegate:(id <WXApiDelegate>) self]|[TencentOAuth HandleOpenURL:url];
}
(1)新浪微博
首先利用 新浪微博提供的物件 呼叫起
WBAuthorizeRequest *request = [WBAuthorizeRequest request]; request.redirectURI = kRedirectURI; request.scope = @"all"; request.userInfo = @{@"myKey": @"myValue"}; [WeiboSDK sendRequest:request];
kRedirectURL 是你在新浪微博 申請的時候 填寫的 url
當我們授權成功之後會在這個 delegate 裡面返回token 和 openId 等資訊
//呼叫起成功之後會在這個方法 能獲取到 token 和 openId 等資訊 -(void)didReceiveWeiboResponse:(WBBaseResponse *)response { APP_DELEGATE.loginVC = nil; if ([response isKindOfClass:WBAuthorizeResponse.class]) { if ((int)response.statusCode == 0) { NSString *toke = [(WBAuthorizeResponse *)response userID]; NSString *openId = [(WBAuthorizeResponse *)response accessToken]; [WBHttpRequest requestWithAccessToken:toke url:@"https://api.weibo.com/2/users/show.json" httpMethod:@"GET" params:[NSDictionary dictionaryWithObject:openId forKey:@"uid"] delegate:(id)self withTag:@"hello_xixi"]; } } }
然後當我們 用token 和 openId 就可以獲取到一些 基本的資訊了
- (void)request:(WBHttpRequest *)request didFinishLoadingWithDataResult:(NSData *)data
{
NSDictionary *content = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];//轉換資料格式
NSLog(@"%@",content); //這裡會返回 一些Base Info
}
還有提供了一些其它的 delegate 方法 用於判斷 基本看名字就知道什麼回事了
- (void)request:(WBHttpRequest *)request didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"%@",response);
}
- (void)request:(WBHttpRequest *)request didFinishLoadingWithResult:(NSString *)result
{
NSLog(@"%@",result);
}
- (void)request:(WBHttpRequest *)request didFailWithError:(NSError *)error
{
NSLog(@"%@",error);
}
跟著後面就可以拿著 這些基本的資訊去根據業務去做一些操作
新浪微博 end
--------------------------------------------我是分割線--------------------------------------------
(2)QQ
首先第一步 我們要用 QQ 提供的物件 呼叫 QQ客戶端
//這個key 有很多 可以根據自己需要去 加入數組裡面
NSArray* permissions = [NSArray arrayWithObjects:
kOPEN_PERMISSION_GET_USER_INFO,
kOPEN_PERMISSION_GET_SIMPLE_USER_INFO,
nil];
_tencentOAuth = [[TencentOAuth alloc] initWithAppId:qAppKey andDelegate:(id)self];
[_tencentOAuth authorize:permissions];
其中的qAppKey 是在申請的時候 有提供的key
跟著就會在 delegate 裡面 獲取到 token 和openId
- (void)tencentDidLogin
{
if (_tencentOAuth.accessToken && 0 != [_tencentOAuth.accessToken length])
{
//成功 之後可以呼叫 getUserInfo
[_tencentOAuth getUserInfo];
}
else
{
//失敗
}
}
成功之後 就可以 繼續呼叫 getUserInfo 這個方法了 ,一看方法名就知道是幹嘛了
那麼 呼叫成功之後會在 下面這個 delegate 方法裡面放回
-(void)getUserInfoResponse:(APIResponse *)response
{
NSLog(@"%@",response);
NSLog(@"%@",response.jsonResponse);
//這裡response 有User Base Info
}
還有一些其它相關的方法 也列出來
- (void)tencentDidNotLogin:(BOOL)cancelled
{
if (cancelled)
{
NSLog(@"取消登入");
}
else
{
NSLog(@"登入失敗");
}
}
- (void)tencentDidNotNetWork
{
NSLog(@"無網路連線,請設定網路");
}
- (void)tencentDidLogout
{
NSLog(@"成功退出登陸");
}
QQ end
--------------------------------------------我是分割線--------------------------------------------
(3) 微信
微信要獲取token 和 openId 跟 新浪微博和QQ 有點區別,它是首先 獲取一個code ,然後跟著這個coed 才能獲取到 token 和 openId
首先 呼叫起 微信客戶端
SendAuthReq *req = [[SendAuthReq alloc] init];
req.scope = @"snsapi_userinfo,snsapi_base"; // 跟QQ 一樣根據自己需要
req.state = wAppState;
req.openID = wAppKey;
[WXApi sendReq:req];
授權回來之後會在 代理方法裡面獲取到code
- (void)onResp:(BaseResp *)resp
{
if (resp.errCode == 0)
{
NSLog(@"%@",resp);
if ([resp isKindOfClass:[SendAuthResp class]])
{
SendAuthResp *sr = (SendAuthResp *)resp;
NSLog(@"%@",sr.code);
[self getAccess_token:sr.code];
}
}
}
要加上型別判斷 因為 分享 也會回撥這個方法,所以要判斷物件型別
那麼獲取到code 之後我們可以根據 提供的url 來獲取到token和openId
-(void)getAccessToken:(NSString *)code
{
NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",wAppKey,wAppSecret,code];
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];
NSLog(@"%@",dic);
NSString *token = [dic objectForKey:@"access_token"];
NSString *openId = [dic objectForKey:@"openid"];
[self getUserInfo:token andOpenId:openId];
}
});
});
}
有了token 和 openId 那麼也能夠獲取到 User Base Info
-(void) getUserInfo:(NSString *)tokenArg andOpenId:(NSString *)openIdArg
{
NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",self.access_token,self.openid];
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];
NSLog(@"%@",dic);
}
});
});
}
微信 end
--------------------------------------------------------------
三、總結
1.QQ 和 新浪微博的 SDK 寫法 差不多,都是授權回來之後就能夠獲取到 token 和 openId
而 微信 得先獲取到一個code 才能獲取 token 和 openId.
2.QQ 和 新浪微博 有提供 代理方法和物件 做了資料封裝,而微信提供一個url 讓開發者自己拼接url ,自己定義方法。(個人比較喜歡 微信的做法)