duangou2046 2014-12-19 09:22
浏览 68
已采纳

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

I want to use a REST service, I need OAuth 1 to allow secure authorization. I have the consumer key, the consumer secret, the token and the token secret. In PHP I'm using this:

$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();

In Objective C I'm using AFOAuth1Client, but I don't know when and how to set the token in the AFOAuth1Client object like $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);
                                                  }];

I think I'm using bad uthorizeUsingOAuthWithRequestTokenPath. How can I parse my PHP code to Objective C. I'm opened to use other OAuth frameworks.

  • 写回答

1条回答 默认 最新

  • douping1581 2015-01-07 11:19
    关注

    Finally I have resolved it using AFOAuth1Client with 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];
    

    I hope this help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗