现在的位置: 首页 > 综合 > 正文

cocos2d-x之新浪微博平台接入接口

2018年02月16日 ⁄ 综合 ⁄ 共 3295字 ⁄ 字号 评论关闭
文章目录

简介

本文档主要介绍接入新浪平台的一些内容,便于查阅和使用。

第一步:认证准备

新浪微博的SDK放在github上面,下载地址:https://github.com/mobileresearch/weibo_ios_sdk_sso-oauth,也可直接在控制台中输入gitclonehttps://github.com/mobileresearch/weibo_ios_sdk_sso-oauth.git将代码下载下来。

将sinaweibo_ios_sdk这个目录添加到工程中,这样可以在代码中使用新浪微博API了。

微博SDK不像facebookSDK那样需要在Info.plist里面设置数据,新浪微博的认证都是通过代码完成的。

 认证代码如下:

  1. bool SinaProxy::init(const char * appKey_, const char * appSecret_, const char * redirectUri_)  
  2. {  
  3.     NSString * appKey = [NSString stringWithUTF8String:appKey_];  
  4.     NSString * appSecret = [NSString stringWithUTF8String:appSecret_];  
  5.     NSString * redirectUri = [NSString stringWithUTF8String:redirectUri_];  
  6.       
  7.     g_sinaDelegate = [SinaDelegate alloc];  
  8.       
  9.     g_sinaWeibo = [[SinaWeibo alloc] initWithAppKey:appKey appSecret:appSecret appRedirectURI:redirectUri andDelegate:g_sinaDelegate];  
  10.     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];  
  11.     NSDictionary *sinaweiboInfo = [defaults objectForKey:@"SinaWeiboAuthData"];  
  12.       
  13.     if ([sinaweiboInfo objectForKey:@"AccessTokenKey"] && [sinaweiboInfo objectForKey:@"ExpirationDateKey"]  
  14.         && [sinaweiboInfo objectForKey:@"UserIDKey"]){  
  15.         g_sinaWeibo.accessToken = [sinaweiboInfo objectForKey:@"AccessTokenKey"];  
  16.         g_sinaWeibo.expirationDate = [sinaweiboInfo objectForKey:@"ExpirationDateKey"];  
  17.         g_sinaWeibo.userID = [sinaweiboInfo objectForKey:@"UserIDKey"];  
  18.     }  
  19.       
  20.     return true;  
  21. }  
  22.   
  23. void SinaProxy::login()  
  24. {  
  25.     [g_sinaWeibo logIn];  
  26. }  

       函数参数为应用的appkey、appsecret以及同样重要的redirecturi,redirecturi的设置详见微博SDK文档。

在初始化完成之后,调用SDK的logIn函数便会弹出官方登陆页面,注意这里设置了一个回调,以接收平台返回的信息,另外这里面加入了一个保存用户session的功能,便于下次自动登陆。

       如果回调函数sinaweiboDidLogIn被调用,则表面登陆成功,可以调用微博的接口了。

 

第二步:获取用户信息

最重要的是第一步,后面的只是调用平台提供的接口得到结果而已。

获取用户信息的接口如下:

  1. void SinaProxy::loadUserInfo(PlatformUserInfo & info)  
  2. {  
  3.     // post status  
  4.     [g_sinaWeibo requestWithURL:@"users/show.json"  
  5.                        params:[NSMutableDictionary dictionaryWithObject:g_sinaWeibo.userID forKey:@"uid"]  
  6.                    httpMethod:@"GET"  
  7.                      delegate:g_sinaDelegate];  
  8. }  

回调部分代码如下:

if ([request.urlhasSuffix:@"users/show.json"]){

        long uid = [result
objectForKey
:@"id"];

        NSString * sname =[resultobjectForKey:@"screen_name"];

    }

第三步:获取好友信息

接口如下:

  1. void SinaProxy::loadFriends(std::vector<PlatformUserInfo*> & friends)  
  2. {  
  3.     [g_sinaWeibo requestWithURL:@"friendships/groups.json"  
  4.                          params:[NSMutableDictionary dictionaryWithObject:g_sinaWeibo.userID forKey:@"uid"]  
  5.                      httpMethod:@"GET"  
  6.                        delegate:g_sinaDelegate];  
  7. }  

注意该接口需要高级权限,需要先申请通过才能获取数据。

 第四步:分享信息

部分代码如下:

  1. [sinaweibo requestWithURL:@"statuses/upload.json"  
  2.                        params:[NSMutableDictionary dictionaryWithObjectsAndKeys:  
  3.                                @"要发布的微博文本内容,必须做URLencode,内容不超过140个汉字http://x-work.org", @"status",  
  4.                                //@"hello world", @"status",  
  5.                                [UIImage imageNamed:@"Icon.png"], @"pic", nil]  
  6.                    httpMethod:@"POST"  
  7.                      delegate:self];  

 

分享采用upload这个接口基本够用了,而且不用申请高级权限,适合大部分情况。另外还有一个接口,upload_url_text,该接口主要是图片地址一个网址,抓取该图片然后分享,需要申请高级权限。

结语

本文档接口与facebook接口保持一致,便于C++开发调用,也方便与其他模块进行整合。

抱歉!评论已关闭.