使用OAuth的iOS推特集成,如Instagram

时间:2014-09-22 11:31:14

标签: ios iphone twitter oauth twitter-oauth

我正在使用twitter实现登录。我希望允许用户从设置登录,如果未配置设置帐户,则使用Web视图登录。以下是我用于在设置中添加帐户的模块。

 ACAccount *newAccount = [[ACAccount alloc]initWithAccountType:accountType];
            ACAccountCredential *credentials = [[ACAccountCredential alloc]initWithOAuthToken:@"" tokenSecret:@""];
            [newAccount setCredential:credentials];
            [accountStore saveAccount:newAccount withCompletionHandler:^(BOOL success, NSError *error) {
                if ( success) {
                    NSLog(@"Success");
                } else {
                    NSLog(@"Fail");
                }
            }];

我尝试通过获取访问令牌使用oAuth登录,但找不到合适的解决方案。 我的OAuth代码如下

twitterURL = @"https://api.twitter.com/oauth/access_token" ;
        NSMutableURLRequest* postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:twitterURL]];
        [postRequest setHTTPMethod: @"POST"];
        NSArray * parameterArray = [NSArray arrayWithObjects:
                                    [NSString stringWithFormat:@"%@=%@", @"x_auth_mode", [@"client_auth" urlEncode]],
                                    [NSString stringWithFormat:@"%@=%@", @"x_auth_password", [TWITTER_PASSWORD urlEncode]],
                                    [NSString stringWithFormat:@"%@=%@", @"x_auth_username", [TWITTER_USERNAME urlEncode]],
                                    nil];
        [postRequest setHTTPBody:[[parameterArray componentsJoinedByString:@"&"] dataUsingEncoding:NSUTF8StringEncoding]];

    [postRequest addValue:self.authorizationHeader
           forHTTPHeaderField:@"Authorization"];

授权标题如下

 NSArray * keysAndValues = [NSArray arrayWithObjects:
                                   [NSString stringWithFormat:@"%@=\"%@\"", @"oauth_nonce", [self.nonce urlEncode]],
                                   [NSString stringWithFormat:@"%@=\"%@\"", @"oauth_signature_method", [@"HMAC-SHA1" urlEncode]],
                                   [NSString stringWithFormat:@"%@=\"%@\"", @"oauth_timestamp", [self.timestamp urlEncode]],
                                   [NSString stringWithFormat:@"%@=\"%@\"", @"oauth_consumer_key", [TWITTERAPIKEY urlEncode]],
                                   [NSString stringWithFormat:@"%@=\"%@\"", @"oauth_signature", [self.signature urlEncode]],
                                   [NSString stringWithFormat:@"%@=\"%@\"", @"oauth_version", [@"1.0" urlEncode]],
                                   nil];
        keysAndValues = [keysAndValues arrayByAddingObject:[NSString stringWithFormat:@"%@=\"%@\"", @"oauth_token", [self.token urlEncode]]];
        return [NSString stringWithFormat:@"OAuth %@", [keysAndValues componentsJoinedByString:@", "]];

在使用上面的OAuth代码发出请求时,它给了我“401错误”

请帮助我解决问题。

0 个答案:

没有答案