【发布时间】:2014-06-01 10:42:40
【问题描述】:
我一直在研究使用完整 OAuth 应用程序流程的所有内容,但我遇到了一个问题,我只能返回 401 =“无效或过期令牌”错误。我已经用文档检查了我的请求,一切看起来都是正确的,我很难过。以下是我的请求的详细信息。
URL
https://api.twitter.com/1.1/oauth/access_token
HTTP Method
POST
Headers
Content-Type: application/x-www-form-urlencoded
Authorization: OAuth oauth_consumer_key="CONSUMER_API_KEY", oauth_nonce="B4D43B0C-A348-4EB6-9C0B-8B0F4FE8", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1397724299", oauth_version="1.0", oauth_token="TOKEN_FROM_REQUEST_TOKEN_API", oauth_signature="ulYIzTacwC%2FeGUdoCPYsrFEqg4A%3D"
HTTP Body
oauth_verifier=OAUTH_VERIFIER_FROM_REQUEST_TOKEN_API
我可以让 oauth/request_token API 正常工作,并且我已经在 Twitter 的应用设置中适当地设置了权限。有谁知道这是怎么回事?
感谢大家提供的任何帮助。
-- 更新--
另外需要注意的是,我正在使用 STTwitter 库来发出请求。它没有内置方法来处理 oauth/authorize 或 oath/authenticate API 方法,所以我使用下面的代码。
// get request token, and present login screen
STTwitterAPI *twitter = [STTwitterAPI twitterAPIWithOAuthConsumerKey:twitterApiKey consumerSecret:twitterApiSecret];
[twitter postTokenRequest:^(NSURL *url, NSString *oauthToken)
{
authWebViewController = [[TWAuthWebViewController alloc] init];
authWebViewController.url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/oauth/authorize?oauth_token=%@", oauthToken]];
authWebViewController.completion = ^(TWAuthWebViewController *authViewController, NSURL *oauthCallbackURL)
{
// get the request token and verifier from the URL
NSString *oauthToken = [TWLoginBusiness getOAuthTokenFromURL:oauthCallbackURL];
NSString *oauthVerifier = [TWLoginBusiness getOAuthVerifierFromURL:oauthCallbackURL];
// get user data with the oauth token
[twitter postResource:@"oauth/access_token"
baseURLString:@"https://api.twitter.com/1.1"
parameters:@{@"oauth_verifier" : oauthVerifier}
uploadProgressBlock:nil
downloadProgressBlock:nil
successBlock:^(NSDictionary *rateLimits, id response)
{
NSLog(@"Reponse: %@", response);
completion(nil, nil);
} errorBlock:^(NSError *error)
{
NSLog(@"Error: %@", error);
completion(nil, error);
}];
};
presentAuthController(authWebViewController);
} oauthCallback:twitterApiCallback errorBlock:^(NSError *error)
{
completion(nil, error);
}];
最后一点。实际显示 Web 视图控制器的部分未在此处列出。我想让这里的代码 sn-p 专注于实际的 API 方法,而不是 UI 逻辑。请确保 authWebViewController.url.... 之后的行显示 web 视图,然后在用户完成 Twitter 网页上的身份验证后调用完成块。另外两个方法 getOauthTokenFromURL 和 getOauthVerifierFromUrl 实际上返回正确的令牌和验证器。 STTwitter 库实际上保存了它自己的令牌,所以这就是为什么它没有手动传递到下面的逻辑中。上面的逻辑生成请求。
谢谢
【问题讨论】:
-
您是否检查过您计算机的系统时间是否准确?如果超过几分钟,可能会导致身份验证问题。
-
我还没有,但这是一个很好的建议,因为前几天我在另一个应用程序上测试了我的日期/时间。我会看看并在这里报告。感谢您的建议:)
-
刚试过,还是不行。我的计算机上的时间设置正确,但我仍然收到 401“Invalid or Expired Token”问题。有什么想法吗?
标签: ios authentication twitter sttwitter