gpt4 book ai didi

iphone - 请求访问 token 的 POST 上缺少无效的 grant_type 参数或参数

转载 作者:太空狗 更新时间:2023-10-30 03:54:04 26 4
gpt4 key购买 nike

我正在努力获取 Quizlet (oauth2) 上的访问 token 。到目前为止一切正常,我可以让用户在 Quizlet 上接受我的应用程序,进行重定向,但是当通过 NSURLConnection 请求访问 token 时,我总是会收到以下错误:

2013-08-17 09:39:33.422 Abiliator[49549:c07] Returned data in string format: {"http_code":400,"error":"invalid_request","error_title":"Not Allowed","error_description":"Invalid grant_type parameter or parameter missing"}

这里是用户身份验证的代码(必须根据规范通过浏览器):

- (void) authenticateQuizletUser
{

NSString *quizletRandomString = [abiliatorAppDelegate GetUUID];
NSString *authURLString = [@"https://quizlet.com/authorize/?response_type=code&client_id=" stringByAppendingString:@"<myID>&scope=read"];
authURLString = [authURLString stringByAppendingString:@"&state="];
authURLString = [authURLString stringByAppendingString:quizletRandomString];
authURLString = [authURLString stringByAppendingString:@"&redirect_uri=Abiliator://after_oauth"];

NSLog(@"Authentication URL sent: %@", authURLString);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: authURLString]];

}

正如我所提到的,这很好用。该应用程序启动 Safari,用户必须确认输入用户 ID 和密码的请求,然后服务器重定向到我的应用程序,我在下面的方法中捕获了它,然后抛出所述错误。

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
if (!url) { return NO; }
NSString *URLString = [url absoluteString];
NSLog(@"Received URL: %@", URLString);
NSString *myURLQuery = [url query];
NSString *myAuthCode = [self getAuthorizationCodeFromURL:myURLQuery];
NSLog(@"Component1: %@", myAuthCode);
NSString *authPasswd = @"myPasswd";
NSString *username=@"myUserName";

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.quizlet.com/oauth/token"]];

request.HTTPMethod = @"POST";

[request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"Abiliator://after_oauth" forHTTPHeaderField:@"redirect_uri"];

// According to Quizlet API doc: You must set this (grant_type) to the string "authorization_code".
[request setValue:@"authorization_code" forHTTPHeaderField:@"grant_type"];
[request setValue:myAuthCode forHTTPHeaderField:@"code"];

NSString *authStr = [NSString stringWithFormat:@"%@:%@", username, authPasswd];
NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedString]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];


return YES; }

非常感谢任何帮助。

最佳答案

OAuth2 defines 4 ways获得访问 token 。最安全和最复杂的是 Authorization Code Grant由 Quizlet 使用,如所述 here .

授权码授予包括两个步骤:

  • 获取授权代码(用户在流通过重定向返回给您之前在您的应用程序外部进行身份验证)
  • 将授权码更改为访问 token

您第一个电话就打对了。第二次调用的问题是您将 grant_type 参数放在请求的错误位置。在这一行中,您将其视为 HTTP header :

[request setValue:@"authorization_code" forHTTPHeaderField:@"grant_type"];

在这里您还将授权代码视为 HTTP header :

[request setValue:myAuthCode forHTTPHeaderField:@"code"];

但是 OAuth2 要求您将两者都放入您的请求正文中。以下是正确请求的示例:

POST /oauth/token/ HTTP/1.1
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
Content-Length: 999
Authorization: Basic xxx

grant_type=authorization_code&code=theCodeYouGotInTheFirstStep&
scope=somescope&redirect_uri=theSameUriYouIncludedEarlier

(空行下面的内容是您的请求正文)
(我在正文中添加换行符只是为了便于阅读——您不能在请求中包含它)

奖励答案:请记住 OAuth2 默认情况下是不安全的:如果您不做一些额外的工作,您的应用程序很容易受到 Cross-Site Request Forgery 的攻击。攻击均匀 mentioned in the OAuth2 RFC .为了防止这种情况,OAuth2 为您提供了 state 参数。您必须为您的 state 生成一个不可猜测的值,并将其包含在第一个请求中。如果服务器返回的 state 与您之前生成的不同,则不得触发第二个请求。

关于iphone - 请求访问 token 的 POST 上缺少无效的 grant_type 参数或参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18286540/

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