实现思路:
微信,微博和qq统称为第三方或第三方平台
app应用登录第三方大致流程是这样:
- 获取code
- 获取author_token
- 获取openid(即用户在该第三方平台的唯一用户标识)(我们的后台)
- 根据openid创建或查询用户,返回app用户信息(我们的后台)
- 余下和正常的用户注册、登录流程相同
其中qq和sina的第三方登录,手机端必须在获取author_token后,才访问服务器端接口,此时的“codeOrToken”的含义是author_token;weixin的第三方登录,手机端必须在获取code后就访问服务器端接口,此时的“codeOrToken”的含义是code
channel是渠道来源,self代表我们自己的渠道,例如放在百度中,则是百度的渠道(仅和安卓相关)
返回中,通过pass的值来判断究竟是在我们的平台中新建了一个用户还是以前第三方登录的用户,如果是新建,则pass不为空,如果不是新建,则pass=""。返回的token,只有是weixin的情况下才有返回。
如果大家在调用第三方平台的其他接口是需要openid,则请大家查找返回的user中的openid和openSource。
代码实现:
[WeiboSDKenableDebugMode:NO];
[WeiboSDKregisterApp:@"appkey"];
[WXApiregisterApp:@"appkey"];
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [WXApihandleOpenURL:url delegate:self] || [WeiboSDKhandleOpenURL:url delegate:self] ||[TencentOAuth HandleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [WXApihandleOpenURL:url delegate:self] || [WeiboSDKhandleOpenURL:url delegate:self] ||[TencentOAuth HandleOpenURL:url];
}
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request
{
}
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response{
NSDictionary *dic = [NSDictionarydictionary];
if ([responseisKindOfClass:WBAuthorizeResponse.class])
{
if ((int)response.statusCode ==0) {
dic =@{@"userID":[(WBAuthorizeResponse *)responseuserID],
@"accessToken" :[(WBAuthorizeResponse *)responseaccessToken]}; }
}
NSLog(@"%@", dic);
NSString * codeStr = [NSStringstringWithFormat:kUrl, str];
AFHTTPSessionManager *man = [AFHTTPSessionManagermanager];
[man.responseSerializersetAcceptableContentTypes:[NSSetsetWithObjects:@"text/plain",nil]];
[manGET:codeStr parameters:nilsuccess:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
NSLog(@"%@", responseObject);
}failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
NSLog(@"%@", error);
}];
}
- (void)onReq:(BaseReq*)req
{
/*
onReq是微信终端向第三方程序发起请求,要求第三方程序响应。第三方程序响应完后必须调用sendRsp返回。在调用sendRsp返回时,会切回到微信终端程序界面。
*/
}
- (void)onResp:(BaseResp*)resp
{
/*
如果第三方程序向微信发送了sendReq的请求,那么onResp会被回调。sendReq请求调用后,会切到微信终端程序界面。
*/
SendAuthResp *aresp = (SendAuthResp *)resp;
if (aresp.errCode==0) {
NSString *code = aresp.code;
NSDictionary *dic = @{@"code":code};
NSString * codeStr = [NSStringstringWithFormat:kUrl, str];
NSLog(@"%@", codeStr);
AFHTTPSessionManager *man = [AFHTTPSessionManagermanager];
[man.responseSerializersetAcceptableContentTypes:[NSSetsetWithObjects:@"text/plain",nil]];
[manGET:codeStr parameters:nilsuccess:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
NSLog(@"%@", responseObject);
}failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
NSLog(@"%@", error);
}];
}
}
//微博登录
- (void)sinaBAction
{
WBAuthorizeRequest *request = [WBAuthorizeRequestrequest];
request.redirectURI =@"授权页";
request.scope =@"all";
[WeiboSDKsendRequest:request];
}
//qq登录
- (void)qqBAction
{
self.auth=[[TencentOAuthalloc]initWithAppId:@"appkey"andDelegate:self];
NSArray * permissions= [NSArrayarrayWithObjects:@"get_user_info",@"get_simple_userinfo",@"add_t",nil];
[self.authauthorize:permissions inSafari:NO];
}
//登陆完成调用
- (void)tencentDidLogin
{
if (self.auth.accessToken && 0 != self.auth.accessToken.length)
{
// 记录登录用户的OpenID、Token以及过期时间
NSString * codeStr = [NSStringstringWithFormat:kUrl, str];
AFHTTPSessionManager *man = [AFHTTPSessionManagermanager];
[man.responseSerializersetAcceptableContentTypes:[NSSetsetWithObjects:@"text/plain",nil]];
[manGET:codeStr parameters:nilsuccess:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
NSLog(@"%@", responseObject);
self.userInfo = [[responseObjectobjectForKey:@"result"]objectForKey:@"user"];
[selfdismissViewControllerAnimated:YEScompletion:^{
[[NSNotificationCenterdefaultCenter]postNotificationName:KLoginAndRegistFinishedNotificationKey
object:nil
userInfo:self.userInfo];
}];
}failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
NSLog(@"%@", error);
}];
}
else
{
NSLog(@"登陆失败");
}
}
//非网络错误导致登录失败:
-(void)tencentDidNotLogin:(BOOL)cancelled
{
NSLog(@"登录失败");
}
// 网络错误导致登录失败:
-(void)tencentDidNotNetWork
{
NSLog(@"无网络连接,请设置网络");
}
//微信登录
- (void)wxBAction
{
SendAuthReq* req =[[SendAuthReqalloc ] init];
req.scope =@"snsapi_userinfo";
req.state =@"weixin";
[WXApisendReq:req];
}