iOS:如何使用给定令牌的 AFOAuth1Client
Posted
技术标签:
【中文标题】iOS:如何使用给定令牌的 AFOAuth1Client【英文标题】:iOS: How to use AFOAuth1Client with given tokens 【发布时间】:2014-12-19 09:22:12 【问题描述】:我想使用 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();
在 Objective 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】:最后我使用 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];
希望对你有所帮助。
【讨论】:
以上是关于iOS:如何使用给定令牌的 AFOAuth1Client的主要内容,如果未能解决你的问题,请参考以下文章
Spotify iOS sdk 使用现有访问令牌连接到 Spotify 远程应用程序
如何使用 Xamarin 在 iOS 13 中获取设备令牌?
如何使用 iOS SDK 保存 LinkedIn 访问令牌?