簡體   English   中英

Twitter上的SLRequest給我這樣的:HTTP響應狀態:403錯誤0錯誤(null)

[英]SLRequest on Twitter give me this: HTTP response status: 403 Error 0error (null)

我正在使用Twitter的SLRequest,直到幾天前一切正常,下面的代碼,但現在響應是空的,錯誤描述為空,這是我總是使用的代碼:

ACAccountStore *accountStoreTw = [[ACAccountStore alloc] init];

ACAccountType *accountTypeTw = [accountStoreTw accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStoreTw requestAccessToAccountsWithType:accountTypeTw options:NULL completion:^(BOOL granted, NSError *error) {
    if(granted) {

        FMDatabase *db = [FMDatabase databaseWithPath:[self databasePath]];

        if ([db open]) {
            FMResultSet *tw_name_q = [db executeQuery:@"SELECT tw_username FROM settings WHERE id = 1"];

            NSString *tw_name = @"";

            while ([tw_name_q next]) {
                tw_name = [tw_name_q stringForColumn:@"tw_username"];
            }

            if (![tw_name isEqualToString:@""]) {

                NSArray *accountsArray = [accountStoreTw accountsWithAccountType:accountTypeTw];

                for (ACAccount *tw_account in accountsArray) {

                    if ([[tw_account username] isEqualToString:tw_name]) {

                        SLRequest* twitterRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                                                       requestMethod:SLRequestMethodPOST
                                                                                 URL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"]
                                                                          parameters:[NSDictionary dictionaryWithObject:msg_post forKey:@"status"]];

                        [twitterRequest setAccount:tw_account];

                        [twitterRequest performRequestWithHandler:^(NSData* responseData, NSHTTPURLResponse* urlResponse, NSError* error) {
                            NSLog(@"text response: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
                            NSLog(@"error: %@",error.localizedDescription);
                            NSString *output = [NSString stringWithFormat:@"HTTP response status: %i Error %d", [urlResponse statusCode],error.code];
                            NSLog(@"%@ error %@", output,error.description);

                        }];

                        break;
                    }
                }
            }
        }

        [db close];
    }

}];

現在這個日志給我這個:

text response: 
error: (null)
403 Error 0 error (null)

問題是什么? 有什么想法來解決它嗎?

Twitter API最近發生了變化 您現在只能使用HTTPS調用它。

您應確保您/您的庫所使用的URL始於

https://api.twitter.com/1.1/

(注意http之后的額外s 。)

如果您在同一個Twitter帳戶上重復測試此代碼,還有另一個原因可以獲得403代碼:

對於每次更新嘗試,都會將更新文本與身份驗證用戶的最新推文進行比較。 任何導致重復的嘗試都將被阻止,從而導致403錯誤。 因此,用戶無法連續兩次提交相同的狀態。

測試時,請務必每次將狀態文本更改為其他文本。 或者您可以編寫簡單的代碼來為狀態生成隨機文本(僅用於調試時間)以防止403錯誤代碼。

例:

NSDictionary *message = @{@"status": [NSString stringWithFormat:@"Testing app %u", arc4random() % 100]};

SLRequest* twitterRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                                                       requestMethod:SLRequestMethodPOST
                                                                                 URL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"]
                                                                          parameters: message];

暫無
暫無

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

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