簡體   English   中英

使用iOS SLRequest更新Twitter個人資料圖片會導致錯誤215錯誤的身份驗證數據

[英]Updating Twitter profile picture with iOS SLRequest results in error 215 bad authentication data

我正在嘗試根據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登出並登錄)。

您的代碼看起來不錯,但是您肯定缺少一些東西。

確保:

  • 您的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]);
         }];
    }
}];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM