1.实现微博第三方登录,首先要在”新浪开发者平台”注册,登录。
http://open.weibo.com
2.创建应用:
微连接->立即创建微连接->移动应用->填写资料(教程可以去以下网址下载)
https://github.com/sinaweibosdk/weibo_ios_sdk
3.下载完成后,看 “微博iOS平台SDK文档V3.1.1.pdf” 这个文档。文档中有如何教你配置文件,但是WeiboSDK支持使用Cocoapods集成,请在Podfile中添加以下语句:
pod "WeiboSDK", :git => "https://github.com/sinaweibosdk/weibo_ios_sdk.git"
跟着”微博iOS平台SDK文档V3.1.1.pdf” 这个文档,完成”一.SDK接入设置”中的 1,2,3,4,8,9,10
至此,文件配置完成。
4.我们需要做的是第三方登录,即: “认证授权”
看”二.应用场景代码示例”中的 “1.SSO微博客户端授权认证”。
5.微博API文档
http://sinaweibosdk.github.io/weibo_ios_sdk/index.html
WeiboSDK->sendRequest->Discussion
( 请求发送给微博客户端程序之后,微博客户端程序会进行相关的处理,处理完成之后一定会调用 [WeiboSDKDelegate didReceiveWeiboResponse:] 方法将处理结果返回给第三方应用)
所以在APPDelegate中,我们还需要遵循 “WeiboSDKDelegate”协议,这两个协议中有两个必须实现的方法,分别是:
didReceiveWeiboRequest:
didReceiveWeiboResponse:
//以下是demo中的示例:
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request
{
}
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
if ([response isKindOfClass:WBSendMessageToWeiboResponse.class])
{
NSString *title = NSLocalizedString(@"发送结果", nil);
NSString *message = [NSString stringWithFormat:@"%@: %d\n%@: %@\n%@: %@", NSLocalizedString(@"响应状态", nil), (int)response.statusCode, NSLocalizedString(@"响应UserInfo数据", nil), response.userInfo, NSLocalizedString(@"原请求UserInfo数据", nil),response.requestUserInfo];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:NSLocalizedString(@"确定", nil)
otherButtonTitles:nil];
WBSendMessageToWeiboResponse* sendMessageToWeiboResponse = (WBSendMessageToWeiboResponse*)response;
NSString* accessToken = [sendMessageToWeiboResponse.authResponse accessToken];
if (accessToken)
{
self.wbtoken = accessToken;
}
NSString* userID = [sendMessageToWeiboResponse.authResponse userID];
if (userID) {
self.wbCurrentUserID = userID;
}
[alert show];
}
else if ([response isKindOfClass:WBAuthorizeResponse.class])
{
NSString *title = NSLocalizedString(@"认证结果", nil);
NSString *message = [NSString stringWithFormat:@"%@: %d\nresponse.userId: %@\nresponse.accessToken: %@\n%@: %@\n%@: %@", NSLocalizedString(@"响应状态", nil), (int)response.statusCode,[(WBAuthorizeResponse *)response userID], [(WBAuthorizeResponse *)response accessToken], NSLocalizedString(@"响应UserInfo数据", nil), response.userInfo, NSLocalizedString(@"原请求UserInfo数据", nil), response.requestUserInfo];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:NSLocalizedString(@"确定", nil)
otherButtonTitles:nil];
self.wbtoken = [(WBAuthorizeResponse *)response accessToken];
self.wbCurrentUserID = [(WBAuthorizeResponse *)response userID];
self.wbRefreshToken = [(WBAuthorizeResponse *)response refreshToken];
[alert show];
}
else if ([response isKindOfClass:WBPaymentResponse.class])
{
NSString *title = NSLocalizedString(@"支付结果", nil);
NSString *message = [NSString stringWithFormat:@"%@: %d\nresponse.payStatusCode: %@\nresponse.payStatusMessage: %@\n%@: %@\n%@: %@", NSLocalizedString(@"响应状态", nil), (int)response.statusCode,[(WBPaymentResponse *)response payStatusCode], [(WBPaymentResponse *)response payStatusMessage], NSLocalizedString(@"响应UserInfo数据", nil),response.userInfo, NSLocalizedString(@"原请求UserInfo数据", nil), response.requestUserInfo];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:NSLocalizedString(@"确定", nil)
otherButtonTitles:nil];
[alert show];
}
else if([response isKindOfClass:WBSDKAppRecommendResponse.class])
{
NSString *title = NSLocalizedString(@"邀请结果", nil);
NSString *message = [NSString stringWithFormat:@"accesstoken:\n%@\nresponse.StatusCode: %d\n响应UserInfo数据:%@\n原请求UserInfo数据:%@",[(WBSDKAppRecommendResponse *)response accessToken],(int)response.statusCode,response.userInfo,response.requestUserInfo];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:NSLocalizedString(@"确定", nil)
otherButtonTitles:nil];
[alert show];
}
}
至此,我们已经实现了第三方登录了!
但是,怎么才能获得相关的数据呢?
//参考如下:
AppDelegate * appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSMutableDictionary * param = [[NSMutableDictionary alloc]initWithCapacity:2];
[param setObject:appdelegate.wbtoken forKey:@"access_token"];
[param setObject:appdelegate.wbCurrentUserID forKey:@"uid"];
NSString * userInfoUrl = @"https://api.weibo.com/2/users/show.json";
[WBHttpRequest requestWithAccessToken:appdelegate.wbtoken url:userInfoUrl httpMethod:@"GET" params:param delegate:self withTag:@"userInfo"];
- (void)request:(WBHttpRequest *)request didFinishLoadingWithDataResult:(NSData *)data
{
//此处不需要使用导入第三方SBJson去解析data也可以
id d = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSDictionary * dict = (NSDictionary *)d;
// NSLog(@"dict:%@",dict);
nameStr = [dict objectForKey:@"name"];
imageUrlStr = [dict objectForKey:@"profile_image_url"];
NSLog(@"name:%@,urlStr:%@",nameStr,imageUrlStr);
}