从 Dropbox OAuth 获取请求令牌 "Invalid OAuth Request"

Getting Request Token From Dropbox OAuth "Invalid OAuth Request"

我正在尝试从 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");
}

您似乎将 "Authorization: OAuth ..." 放入 POST 请求的 body 而不是将 "OAuth ..." 放入 Authorization header.

试试这个代码(抱歉,未经测试):

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。它要简单得多。