Invalid grant_type parameter or parameter missing on POST for requesting
access token
I am struggling to get the access token on Quizlet (oauth2). Everything
works fine so far, I can make the user accepting my app on Quizlet, get
redirected, but when requesting the access token via NSURLConnection, I
always get the following error:
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"}
This is the method throwing the error:
- (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; }
Any help hihgly appreciated.
No comments:
Post a Comment