使用新浪微博开放平台api同步微博内容至自己网站(文档_V23.API错误代码说明()(图))
优采云 发布时间: 2021-12-30 03:19使用新浪微博开放平台api同步微博内容至自己网站(文档_V23.API错误代码说明()(图))
文档_V2
3.API 错误码描述地址:
4.iOS SDK 地址:
5.授权机制:
授权机制说明
6.开发者管理中心
利用:
在管理中心创建自己的应用后,会得到A*敏*感*词*ey和App Secret
这两个值是初始化新浪微博SDK必须用到的两个参数。
执行登录功能时可能遇到的错误如下
1:访问错误提示
表示:微博SDK初始化时设置的appRedirectURI和微博开放平台-开发者管理中心-应用信息-高级信息-OAuth2.0 授权设置-授权回调页面
如果设定值不同,就会出现上述错误。
2:调用新浪微博客户端授权后,应用没有正常返回。
1:检查URL类型“URL方案”是否命名为:sinaweibosso。如图添加AppID:
2:在AppDelegate中是否实现了委托功能:
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSLog(@"%@",url.scheme);
//如果涉及其他应用交互,请做如下判断,例如:还可能和新浪微博进行交互
if ([url.scheme isEqualToString:Key_weiXinAppID]) {
return [WXApi handleOpenURL:url delegate:self];
}else if ([url.scheme isEqualToString:[@"sinaweibosso" stringByAppendingPathExtension:Key_sinaWeiboAppID]])
{
return [[SinaWeiBoManage defaultInstance].sinaWeibo handleOpenURL:url];
}else
{
return YES;
}
}
完成上述设置后,将无意外地响应授权结果。
接下来,我们主要开始调用API与微博数据进行交互。
举个简单的例子,如何获取授权用户的个人信息:
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:userId forKey:@"uid"];
[sinaWeibo requestWithURL:@"users/show.json"
params:params
httpMethod:@"GET"
delegate:self];
具体需要传入的参数请参考官方API文档。
得到结果后,会响应委托成功或失败:
这时候可以通过链接名来识别请求类型:
- (void)request:(SinaWeiboRequest *)request didFailWithError:(NSError *)error
{
//获取关注列表
if ([request.url hasSuffix:@"friendships/friends.json"])
{
if ([delegate respondsToSelector:@selector(sinaWeiBoManage:withFriendListResult:withRequestDataType:isSuccess:)]) {
[delegate sinaWeiBoManage:self withFriendListResult:nil withRequestDataType:self.requestDataType isSuccess:NO];
}
}
}
关于iOS 6内置微博功能:
在iOS6中,苹果集成了新浪微博的社交环境,所以如果用户在设置界面对新浪微博账号进行了授权,就可以直接在我们的第三方应用中使用,用于发布微博等。
先介绍两个新框架
他们是:
Accounts.framework:用于在系统设置中获取账户信息
Social.framework:用于与第三方开放平台进行数据交互。
该过程分为两个步骤:
首先要知道用户是否在系统设置中对相应的账号进行了授权。
获取到相应的账号信息后,即可开始与第三方开放平台进行数据交互,代码如下:
// Create an account store object. 创建账户集合
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved. 确定好 账户类型 新浪微博 还是 Facebook 还是 Twitter
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierSinaWeibo];
// Request access from the user to use their Twitter accounts. //异步请求 来得到对应类型的账户信息
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {//如果 granted 返回 NO 表示一个账户都没有设置
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; //可能存在多个,看你要用哪个,最好让用户选择一下
// For the sake of brevity, we'll assume there is only one Twitter account present.
// You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *sinaWeiboAccount = [accountsArray objectAtIndex:0];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:@"一条新的微博" forKey:@"status"];
SLRequest *slRequest = [SLRequest requestForServiceType:SLServiceTypeSinaWeibo requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://open.weibo.cn/2/statuses/update.json"] parameters:params];
slRequest.account = sinaWeiboAccount;//这行代码一定要赋值,负责数据交互一定失败
[slRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(@"%@ %@",urlResponse.URL,error);
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
}];
}
}
}];
获得每个 ACAccount 后,它就有了一个标识符。最好在用户确认使用哪个账号的时候保存,这样下次可以直接通过下面的代码获取对应的账号
[accountStore accountWithIdentifier:sinaWeiboAccount.identifier];
总结:
SNS作为应用推广的主要方式,必须认真研究。如何唤起和激发用户的分享欲望,让更多人知道你的应用,那么成功就在不远处。