Getting Request Token From Dropbox OAuth “Invalid

2019-09-17 06:16发布

问题:

I'm trying to get a response data from dropbox oauth as described here Now i dont know why it keeps saying {"error": "Invalid OAuth request."}

Here is a my complete code for requesting token:

-(void)requestTokenSecret
{

  NSString* oauthVersion=@"1";
  NSString* oauth_signature_method=@"PLAINTEXT";


  NSString* postHeader = [NSString stringWithFormat: @"Authorization: OAuth oauth_version=\"%@\", oauth_signature_method=\"%@\", oauth_consumer_key=\"%@\", oauth_signature=\"%@&\"",oauthVersion,oauth_signature_method,APP_KEY,APP_SECRET];
  NSLog(@"URL HEADER: %@",postHeader);


  NSData* postHeaderData = [postHeader dataUsingEncoding:NSUTF8StringEncoding]; 

  NSMutableURLRequest* request= [[NSMutableURLRequest alloc]init];

  [request setURL:[NSURL URLWithString:@"https://api.dropbox.com/1/oauth/request_token"]];
  [request setHTTPMethod:@"POST"];

  [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

  [request setValue:[NSString stringWithFormat:@"%d",[postHeaderData length]] forHTTPHeaderField:@"Content-Length"];
  [request setHTTPBody:postHeaderData];

  NSURLConnection* conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
  [conn start];
  if(conn)
  {
    NSLog(@"Connected");
  }
  else
    NSLog(@"Connected");
}

回答1:

It looks like you're putting "Authorization: OAuth ..." in the body of the POST request instead of putting "OAuth ..." in the Authorization header.

Give this code a try instead (untested, sorry):

NSString* oauthVersion=@"1";
NSString* oauth_signature_method=@"PLAINTEXT";

NSString* authorizationHeader = [NSString stringWithFormat: @"OAuth oauth_version=\"%@\", oauth_signature_method=\"%@\", oauth_consumer_key=\"%@\", oauth_signature=\"%@&\"",oauthVersion,oauth_signature_method,APP_KEY,APP_SECRET];

NSMutableURLRequest* request= [[NSMutableURLRequest alloc]init];

[request setURL:[NSURL URLWithString:@"https://api.dropbox.com/1/oauth/request_token"]];
[request setHTTPMethod:@"POST"];

[request setValue:authorizationHeader forHTTPHeaderField:@"Authorization"];

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

Also, I would strongly suggest using OAuth 2 instead of OAuth 1. It's considerably simpler.