0

我正在 iOS 上编写一个简单的应用程序,并且我正在尝试使用 RESTFull Web 服务为了获得一些技巧并获得一些知识,我首先从 twitter 开始,而不使用 ios 集成库,但我总是在 Auth 上备货。

任何工作示例或链接将不胜感激,因为提醒 twitter 不是我的最终目标

例如,这是我的代码之一,基于 RestKit 和 AFOAuth1Client :

    NSURL *url = [NSURL URLWithString:@"https://api.twitter.com"];

AFOAuth1Client * twitterClient = [[AFOAuth1Client alloc] initWithBaseURL:url key:kCLIENTID secret:kCLIENTSECRET];

[twitterClient authorizeUsingOAuthWithRequestTokenPath:@"oauth/request_token"
                                 userAuthorizationPath:@"oauth/authorize"
                                           callbackURL:[NSURL URLWithString:@"af-twitter://success"]
                                       accessTokenPath:@"oauth/access_token"
                                          accessMethod:@"POST"
                                                 scope:@""
                                               success:^(AFOAuth1Token *token, id responseObject) {
                                               }
                                               failure:^(NSError *error) {

                                               }];

如您所见,它不起作用:

    2013-06-17 16:59:01.389 restKitTest[14490:c07] I restkit:RKLog.m:34 RestKit logging initialized...
2013-06-17 16:59:02.653 restKitTest[14490:c07] I restkit.network:RKObjectRequestOperation.m:174 POST 'http://api.twitter.com/oauth/request_token'
2013-06-17 16:59:03.051 restKitTest[14490:c07] E restkit.network:RKObjectRequestOperation.m:203 POST 'http://api.twitter.com/oauth/request_token' (401 Unauthorized) [0.3980 s]: Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 401" UserInfo=0x9c96640 {NSLocalizedRecoverySuggestion=Failed to validate oauth signature and token, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0x9877e20> { URL: http://api.twitter.com/oauth/request_token }, NSErrorFailingURLKey=http://api.twitter.com/oauth/request_token, NSLocalizedDescription=Expected status code in (200-299), got 401, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x9aac910> { URL: http://api.twitter.com/oauth/request_token } { status code: 401, headers {
    "Cache-Control" = "no-cache, no-store, must-revalidate, pre-check=0, post-check=0";
    "Content-Encoding" = gzip;
    "Content-Length" = 62;
    "Content-Type" = "text/html; charset=utf-8";
    Date = "Mon, 17 Jun 2013 14:59:03 GMT";
    Expires = "Tue, 31 Mar 1981 05:00:00 GMT";
    "Last-Modified" = "Mon, 17 Jun 2013 14:59:03 GMT";
    Pragma = "no-cache";
    Server = tfe;
    "Set-Cookie" = "_twitter_sess=BAh7CDoPY3JlYXRlZF9hdGwrCOJ3pVI%252FAToHaWQiJTIyYzUxN2UwNmUwYTI4%250AYzU1NmU0OTYxZDA5MDcwMmI3IgpmbGFzaElDOidBY3Rpb25Db250cm9sbGVy%250AOjpGbGFzaDo6Rmxhc2hIYXNoewAGOgpAdXNlZHsA--3291687666d367b6ccddc74513532bf56ea144d9; domain=.twitter.com; path=/; HttpOnly, guest_id=v1%3A137148114312677519; Domain=.twitter.com; Path=/; Expires=Wed, 17-Jun-2015 14:59:03 UTC";
    Status = "401 Unauthorized";
    Vary = "Accept-Encoding";
    "x-frame-options" = SAMEORIGIN;
    "x-mid" = af106773611449e1226d5dd5d531b64d91e4f3f8;
    "x-runtime" = "0.01153";
    "x-transaction" = 24e58d8eb3b7cdb5;
    "x-ua-compatible" = "IE=10,chrome=1";
    "x-xss-protection" = "1; mode=block";
} }}
4

2 回答 2

0

“401 Unauthorized”表示您的凭据不正确。因此,请检查您的 API 密钥。

于 2013-07-04T13:18:40.357 回答
0

尝试这个:

[twitterClient authorizeUsingOAuthWithRequestTokenPath:@"/oauth/request_token"
                                     userAuthorizationPath:@"/oauth/authorize"
                                               callbackURL:[NSURL URLWithString:@"instantshare-twitter://success"]
                                           accessTokenPath:@"/oauth/access_token"
                                              accessMethod:@"POST"
                                                     scope:nil
                                                   success:^(AFOAuth1Token *accessToken, id responseObject) {
                                                       [twitterClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
                                                   } failure:^(NSError *error) {
                                                       NSLog(@"Error: %@", error);
                                                   }];

请注意这一行scope:nil,由于某种原因,它不适用于空字符串。

除其他事项外,请检查您在 twitter 中的应用程序设置。我不确定是否是这种情况,但我启用Allow this application to be used to Sign in with Twitter并将回调 URL设置为一些随机的有效 url

于 2014-11-14T15:02:49.313 回答