【问题标题】:Updating Twitter profile picture with iOS SLRequest results in error 215 bad authentication data使用 iOS SLRequest 更新 Twitter 个人资料图片会导致错误 215 错误的身份验证数据
【发布时间】:2016-04-18 04:59:45
【问题描述】:

我正在尝试根据 Twitter 的文档https://dev.twitter.com/rest/reference/post/account/update_profile_image 发布新的个人资料图片,这是我的代码:

NSData *jpeg = UIImageJPEGRepresentation(img, 0.9);
NSString *base64 = [jpeg base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/account/update_profile_image.json"];
NSDictionary *params = @{@"image" : base64
                         };
SLRequest *request =
[SLRequest requestForServiceType:SLServiceTypeTwitter
                   requestMethod:SLRequestMethodPOST
                             URL:url
                      parameters:params];
[request setAccount:self.twitterAccount];
[request performRequestWithHandler:^(NSData *responseData,
                                     NSHTTPURLResponse *urlResponse,
                                     NSError *error) {
    ...
}

但是 URL 响应是:

errors =     (
            {
        code = 215;
        message = "Bad Authentication data.";
    }
);

我做错了什么?我的ACAccount 有效(我刚刚尝试在 Settings.app 中退出和登录 Twitter)。

【问题讨论】:

标签: ios twitter acaccount slrequest


【解决方案1】:

您的代码看起来不错,但您一定遗漏了一些东西。

确保:

  • 您的 Twitter 帐户已连接并列在“设置”应用程序 Twitter 设置中。我知道您已经检查过了,但您必须验证它。甚至可以重新启动您的手机,然后确保您的 Twitter 句柄在那里。
  • 您的应用已请求并授予权限 访问帐户(使用requestAccessToAccountsWithType 方法)。授予权限后,您应该会在“允许这些应用程序使用您的帐户”下的 Twitter 设置(在“设置”应用程序中)中看到您的应用程序。
  • 您设置为 SLRequest 的 Twitter 帐户有效,但不是 nil

鉴于上述条件,我刚才能够成功修改我的个人资料图片。以下是基于您提供的代码的示例代码:

ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
{
    if (granted)
    {
        NSArray *accounts = [accountStore accountsWithAccountType:accountType];
        ACAccount *twitterAccount = [accounts lastObject];

        NSData *jpeg = UIImageJPEGRepresentation([UIImage imageNamed:@"photo.jpg"], 0.9);
        NSString *base64 = [jpeg base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
        SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                                requestMethod:SLRequestMethodPOST
                                                          URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/account/update_profile_image.json"]
                                                   parameters:@{@"image" : base64}];
        [request setAccount:twitterAccount];
        [request performRequestWithHandler:^(NSData *responseData,
                                             NSHTTPURLResponse *urlResponse,
                                             NSError *error)
         {
             NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil]);
         }];
    }
}];

【讨论】:

  • 这是我的错。我不知道我是怎么犯下这么简单的错误的,但我的self.twitterAccount 是零。在我的一些测试中,它不是,我不知道为什么它当时不起作用。但后来它总是为零。在那里获得一个有效的 Twitter 帐户对象后,它就起作用了。
  • 能否请您在有关检查 twitterAccount 是否为 nil 的答案中添加一些更新,这是我问题的实际解决方案,所以我可以奖励您赏金?
  • @CanPoyrazoğlu 已完成(已添加到“要确保的事项”列表中)。谢谢! :)
猜你喜欢
  • 2013-10-05
  • 1970-01-01
  • 2012-09-22
  • 2013-01-02
  • 2015-07-10
  • 1970-01-01
  • 2019-07-24
  • 2011-06-13
  • 2014-10-15
相关资源
最近更新 更多