使用iOS SLRequest更新Twitter配置文件图片会导致错误215错误身份验证数据

使用iOS SLRequest更新Twitter配置文件图片会导致错误215错误身份验证数据,ios,twitter,acaccount,slrequest,Ios,Twitter,Acaccount,Slrequest,我正试图根据Twitter的文档在发布新的个人资料图片,以下是我的代码: NSData *jpeg = UIImageJPEGRepresentation(img, 0.9); NSString *base64 = [jpeg base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1

我正试图根据Twitter的文档在发布新的个人资料图片,以下是我的代码:

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注销并登录到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.twitter帐户
为零。在我的一些测试中,它不是,我不知道当时为什么不起作用。但后来总是零。在那里获得了一个有效的Twitter帐户对象后,它起了作用。请您在回答中添加一个关于检查twitterAccount是否为零的更新,这是我问题的实际解决方案,以便我可以奖励您奖金?@CanPoyrazoğlu done(添加到“确保事项”列表中)。谢谢!:)