【问题标题】:post tweet using gtm-oauthauthentication使用 gtm-oauthauthentication 发布推文
【发布时间】:2013-09-13 17:00:22
【问题描述】:

我已使用 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"]); 
         } 
     }]; 

谁能告诉我这种方法我做错了什么?

提前致谢。

【问题讨论】:

    标签: ios objective-c twitter


    【解决方案1】:

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

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

    【讨论】:

    • 你能告诉我如何发布链接和图片吗?
    猜你喜欢
    • 2013-10-20
    • 2021-04-11
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多