iOS:如何将AFOAuth1Client与给定的令牌一起使用

时间:2014-12-19 09:22:13

标签: php ios oauth afnetworking

我想使用REST服务,我需要OAuth 1来允许安全授权。 我有消费者密钥,消费者秘密,令牌和令牌秘密。 在PHP中,我使用它:

$apiUrl = 'http://www.web.com/api/rest';
$consumerKey = 'xxxx';
$consumerSecret = 'yyyyyy';
$token = 'zzzzz';
$tokenSecret = 'wwwww';

session_start();

$authType = OAUTH_AUTH_TYPE_AUTHORIZATION;  
$oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
$oauthClient->enableDebug();

$oauthClient->setToken($token, $tokenSecret);
$resourceUrl = "$apiUrl"; 
$headers = array('Content-Type' => 'application/json');

$oauthClient->fetch($resourceUrl.'/category/2681', array(), OAUTH_HTTP_METHOD_GET, $headers);

$productsList = $oauthClient->getLastResponse();

在目标C中我使用AFOAuth1Client,但我不知道何时以及如何在AFOAuth1Client对象中设置令牌,如$ oauthClient-> setToken($ token,$ tokenSecret);

AFOAuth1Client *oAuth1Client = [[AFOAuth1Client alloc] initWithBaseURL:[NSURL URLWithString:kUrlPre] key:@"consumerKey" secret:@"consumerSecret"];

oAuth1Client.signatureMethod = AFHMACSHA1SignatureMethod;
oAuth1Client.accessToken = [[AFOAuth1Token alloc] initWithKey:@"consumerKey" secret:@"consumerSecret" session:nil expiration:nil renewable:YES]; //I have tried this, but when I add this line I'm getting this error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: oauth_token)' in authorizeUsingOAuthWithRequestTokenPath 

[oAuth1Client authorizeUsingOAuthWithRequestTokenPath:@"http://www.web.com/api/rest/category/2681"
                                    userAuthorizationPath:nil //??
                                              callbackURL:nil //I don't need it
                                          accessTokenPath:nil //I don't need it
                                             accessMethod:@"GET"
                                                    scope:nil 
                                                  success:^(AFOAuth1Token *accessToken, id responseObject) {
                                                      NSLog(@"Success: %@", accessToken);
                                                  } failure:^(NSError *error) {
                                                      NSLog(@"Error: %@", error);
                                                  }];

我认为我使用了糟糕的uthorizeUsingOAuthWithRequestTokenPath。如何将我的PHP代码解析为Objective C.我打开了使用其他OAuth框架。

1 个答案:

答案 0 :(得分:1)

最后,我使用AFOAuth1Client和AFHTTPRequestOperation解决了它:

_oAuth1Client = [[AFOAuth1Client alloc] initWithBaseURL:[NSURL URLWithString:kUrlPre] key:kConsumerKey secret:kConsumerSecret];

[_oAuth1Client setDefaultHeader:@"Accept" value:@"application/json"];
[_oAuth1Client setSignatureMethod:AFHMACSHA1SignatureMethod];
[_oAuth1Client setAccessToken:[[AFOAuth1Token alloc] initWithKey:kToken secret:kTokenSecret session:nil expiration:nil renewable:FALSE]];
[_oAuth1Client registerHTTPOperationClass:[AFHTTPRequestOperation class]];

NSMutableURLRequest * request =[_oAuth1Client requestWithMethod:@"GET" path:[NSString stringWithFormat:@"%@", kUrlPre] parameters:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
    NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject     encoding:NSUTF8StringEncoding]);
}
    failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
    NSLog(@"Error: %@", error);
}];
[operation start];

我希望这对你有所帮助。

相关问题