gpt4 book ai didi

php - iOS:如何使用给定 token 的 AFOAuth1Client

转载 作者:行者123 更新时间:2023-11-29 02:25:41 25 4
gpt4 key购买 nike

我想使用 REST 服务,我需要 OAuth 1 来允许安全授权。我有消费者 key 、消费者 secret 、 token 和 token secret 。在 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 对象中设置 token ,例如 $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 框架。

最佳答案

最后,我使用带有 AFHTTPRequestOperation 的 AFOAuth1Client 解决了这个问题:

_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];

希望对你有帮助

关于php - iOS:如何使用给定 token 的 AFOAuth1Client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27562917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com