如何在iOS中使用Twitter API 1.1获取令牌

时间:2013-12-07 06:03:14

标签: ios objective-c twitter-bootstrap twitter

我正在使用“ STTwitter ”从以下网址获取令牌并请求正文

https://dev.twitter.com/docs/api/1.1/post/oauth2/token

请求包含URL的正文代码

- (void)verifyCredentialsWithSuccessBlock:(void(^)(NSString *username))successBlock errorBlock:(void(^)(NSError *error))errorBlock {

    [self postResource:@"oauth2/token"
         baseURLString:@"https://api.twitter.com"
            parameters:@{ @"grant_type" : @"client_credentials" }
          useBasicAuth:YES
   uploadProgressBlock:nil
 downloadProgressBlock:nil
          successBlock:^(id request, NSDictionary *requestHeaders, NSDictionary *responseHeaders, id json) {

              NSString *tokenType = [json valueForKey:@"token_type"];
              if([tokenType isEqualToString:@"bearer"] == NO) {
                  NSError *error = [NSError errorWithDomain:NSStringFromClass([self class]) code:STTwitterAppOnlyCannotFindBearerTokenInResponse userInfo:@{NSLocalizedDescriptionKey : @"Cannot find bearer token in server response"}];
                  errorBlock(error);
                  return;
              }

              self.bearerToken = [json valueForKey:@"access_token"];

              successBlock(_bearerToken);

          } errorBlock:^(id request, NSDictionary *requestHeaders, NSDictionary *responseHeaders, NSError *error) {
              errorBlock(error);


              NSLog(@"ERROR %@",[error description]);

          }];
}

对于Calling上面的方法,我正在进行下面的代码

STTwitterAppOnly *twitter1 = [[STTwitterAppOnly alloc] init];
    [twitter1 verifyCredentialsWithSuccessBlock:^(NSString *bearerToken) {

        NSLog(@"sd");

        [self.twitter getUserTimelineWithScreenName:@"SEREEN_NAME"
                                  successBlock:^(NSArray *statuses) {

                                      NSLog(@"ERROR ::: %@",[statuses description]);

                                      // ...
                                  } errorBlock:^(NSError *error) {
                                      // ...
                                  }];

    } errorBlock:^(NSError *error) {
        // ...
    }];

执行上面的代码时,我得到了以下错误...

**Error Domain=STHTTPRequest Code=99 "Unable to verify your credentials"** 

你能告诉我我的代码中遗漏了什么吗?

我担心的是,我想在Twitter上没有登录就阅读twitter feed。只使用Twitter API V1.1的“Consumer Key”和“Secret Key”。

由于

3 个答案:

答案 0 :(得分:2)

如果您有ConsumerKey和ConsumerSecret,则可以尝试

STTwitterAPIWrapper *twitterAPI = [[STTwitterAPIWrapper twitterAPIApplicationOnlyWithConsumerKey:@"Your Consumer Key" consumerSecret:@"Your Consumer Secret"] autorelease];
[twitterAPI verifyCredentialsWithSuccessBlock:^(NSString *username) {
     [twitterAPI getUserTimelineWithScreenName:@"your ScreenName" count:25 successBlock:^(NSArray *statuses) {
         NSLog(@"Success:%@", statuses);

     } errorBlock:^(NSError *error){
         NSLog(@"Error : %@",error.description);
     }];

} errorBlock:^(NSError *error) {
         NSLog(@"Error : %@",error.description);
}];

答案 1 :(得分:0)

验证您是否在plist和header前缀中输入了Consumer密钥和Consumer Secret ...

答案 2 :(得分:0)

您发布的代码有几个问题。首先,您是从验证方法错误块而不是成功块发出请求。其次,你直接使用postResource方法,而不是更高级别的方法来做你想要的。

如果您想要的只是消费者令牌的持有人令牌,您只需从App Only Authentication部分的STTwitter自述文件中复制/粘贴示例代码即可。