0

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");
}
user94559
  • 59,196
  • 6
  • 103
  • 103
MetaSnarf
  • 5,857
  • 3
  • 25
  • 41

1 Answers1

0

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.

user94559
  • 59,196
  • 6
  • 103
  • 103
  • follow up question. is it possible to skip the user authorization? (step 2 [here]) ? im using only 1 static account as also described in my question [here]. [1]https://www.dropbox.com/developers/blog/20/using-oauth-10-with-the-plaintext-signature-method [2]http://stackoverflow.com/questions/27834922/auto-login-dropbox-account-on-core-api-without-login-prompt – MetaSnarf Jan 14 '15 at 03:59
  • It looks like that Stack Overflow question has already been answered correctly. You almost certainly don't want to do this, but the answer tells you how to do it if you persist. – user94559 Jan 14 '15 at 04:02
  • the method `[dbSession updateAccessToken:APP_ACCESS_TOKEN accessTokenSecret:APP_SECRET forUserId: APP_USER_EMAIL ];` requires access token and access token secret. When i tried to convert request token to get access token it says 'Request token has not been properly authorized' but i want to skip showing user the authorization page. (step 2 in https://www.dropbox.com/developers/blog/20/using-oauth-10-with-the-plaintext-signature-method ) – MetaSnarf Jan 14 '15 at 04:06