从Dropbox OAuth获取请求令牌“无效的OAuth请求”

时间:2015-01-13 08:12:58

标签: oauth dropbox dropbox-api

我试图从Dropbox oauth获取响应数据,如here所述 现在我不知道为什么它一直说{"error": "Invalid OAuth request."}

以下是我提供令牌的完整代码:

-(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 个答案:

答案 0 :(得分:0)

看起来您在POST请求的正文中放置了“授权:OAuth ...”,而不是在Authorization标题中添加“OAuth ...”。

尝试使用此代码(未经测试,抱歉):

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];

另外,我强烈建议使用OAuth 2而不是OAuth 1.它相当简单。