使用gtm-oauthauthentication发布推文

时间:2013-09-13 08:45:33

标签: ios objective-c twitter

我已经使用GTMOAuthAuthentication进行Twitter身份验证,并且我已成功使用以下代码登录Twitter。

NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/request_token"];
NSURL *accessURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/access_token"];
NSURL *authorizeURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/authorize"];
NSString *scope = @"http://api.twitter.com/";
GTMOAuthAuthentication * auth = [self authForTwitter];
[auth setCallback:@"http://www.noop.com/OAuthCallback"];
[auth setCallback:@"http://www.noop.com/OAuthCallback"];
GTMOAuthViewControllerTouch *viewController = [[GTMOAuthViewControllerTouch alloc]   
initWithScope:scope
language:nil 
requestTokenURL:requestURL  
authorizeTokenURL:authorizeURL
accessTokenURL:accessURL
authentication:auth
appServiceName:@"AppName : Twitter"
delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)];

[self.navigationController pushViewController:viewController animated:YES];

我在验证后获得了访问令牌。现在我想将文字,图片和网址发布到twitter。如果我使用以下代码,我得到410错误代码

    NSString *body = [NSString stringWithFormat: @"status=thisisatest"];
    NSString *urlStr = @"http://api.twitter.com/1/statuses/update.json";
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody: [body dataUsingEncoding:NSUTF8StringEncoding]];
    GTMHTTPFetcher* myFetcher = [GTMHTTPFetcher
                                 fetcherWithRequest:request];
    [myFetcher setAuthorizer: auth];
    [myFetcher beginFetchWithCompletionHandler:^(NSData *retrievedData,
                                                 NSError *error)
     {
         if (error != nil)
         {
             NSLog(@"POST error: %@", error);
         }
         else
         {
             NSDictionary *results = [[[[NSString alloc] initWithData:
                                        retrievedData encoding:NSUTF8StringEncoding] autorelease] JSONValue];
             NSLog(@"POST Successful: #%@ @ %@", [results objectForKey:
                                                  @"id"], [results objectForKey: @"created_at"]); 
         } 
     }]; 

任何人都能告诉我这个方法我做错了吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

使用1.1 API。现在认为版本1已弃用,您也可以在文档中阅读。

https://api.twitter.com/1.1/statuses/update.json