尝试获取Twitter令牌

时间:2014-01-18 04:48:46

标签: ios objective-c twitter

这是我的代码:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://twitter.com/oauth/request_token?oauth_callback=http://musiclamhe.com&oauth_consumer_key=jxSd6mnfXZoijjB9LppvTZA&oauth_nonce=28dc0bec380eba99306b7ff256e7bc67&&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1389960513&oauth_version=1.0"]
                                                        cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                       timeoutInterval:100];

   [request setHTTPMethod: @"GET"];

   NSError *requestError;
  NSURLResponse *urlResponse = nil;


   NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
     NSLog(@"ooo %@",response1);
      NSString *nss=[[NSString alloc]initWithData:response1 encoding:NSUTF8StringEncoding];
      NSLog(@"ooo %@",nss);

它显示以下错误消息:

failed to validate oauth signature and token

1 个答案:

答案 0 :(得分:0)

试试这样。更多信息here

NSString *S = resultOfStep1;
    NSDictionary *step2Params = [[NSMutableDictionary alloc] init];
    [step2Params setValue:@"JP3PyvG67rXRsnayOJOcQ"
                   forKey:@"x_reverse_auth_target"];

    [step2Params setValue:S forKey:@"x_reverse_auth_parameters"];

    NSURL *url2 =
      [NSURL URLWithString:@"https://api.twitter.com/oauth/access_token"];

    SLRequest *stepTwoRequest =
      [SLRequest requestForServiceType:SLServiceTypeTwitter
                         requestMethod:SLRequestMethodPOST
                                   URL:url2
                            parameters:step2Params];

    // You *MUST* keep the ACAccountStore alive for as long as you need an
    // ACAccount instance. See WWDC 2011 Session 124 for more info.
    self.accountStore = [[ACAccountStore alloc] init];

    //  We only want to receive Twitter accounts
    ACAccountType *twitterType =
      [self.accountStore accountTypeWithAccountTypeIdentifier:
        ACAccountTypeIdentifierTwitter];

    //  Obtain the user's permission to access the store
    [self.accountStore requestAccessToAccountsWithType:twitterType
    withCompletionHandler:^(BOOL granted, NSError *error) {
        if (!granted) {
            // handle this scenario gracefully
        } else {
            // obtain all the local account instances
            NSArray *accounts =
            [self.accountStore accountsWithAccountType:twitterType];

            // for simplicity, we will choose the first account returned - in
            // your app, you should ensure that the user chooses the correct
            // Twitter account to use with your application.  DO NOT FORGET THIS
            // STEP.
            [stepTwoRequest setAccount:[accounts objectAtIndex:0]];

            // execute the request
            [stepTwoRequest performRequestWithHandler:^
              (NSData *responseData,
              NSHTTPURLResponse *urlResponse,
              NSError *error) {
                  NSString *responseStr =
                    [[NSString alloc] initWithData:responseData
                                          encoding:NSUTF8StringEncoding];

                  // see below for an example response
                  NSLog(@"The user's info for your server:\n%@", responseStr);
            }];
        }
    }];
相关问题