2015-01-13 130 views
0

我試圖描述here 現在,我不知道爲什麼口口聲聲說{"error": "Invalid OAuth request."}獲取請求令牌從Dropbox的OAuth的「無效的OAuth請求」

這裏是一個我的完整代碼從Dropbox的OAuth的響應數據請求令牌:

-(void)requestTokenSecret 
{ 

    NSString* [email protected]"1"; 
    NSString* [email protected]"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"); 
} 

回答

0

看起來你把「授權:OAuth的......」在POST請求而不是把身體「OAuth的......」在Authorization頭。

給這個代碼一試,而不是(未經測試,不好意思):

NSString* [email protected]"1"; 
NSString* [email protected]"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,而不是1的OAuth這是相當簡單。

+0

你是上帝!謝謝senpai ... – MetaSnarf

+0

跟進問題。是否有可能跳過用戶授權? (第2步[這裏])?即時通訊只使用一個靜態帳戶,也在我的問題[這裏]中描述。 [1] https://www.dropbox.com/developers/blog/20/using-oauth-10-with-the-plaintext-signature-method [2] http://stackoverflow.com/問題/ 27834922 /自動登錄-的Dropbox帳戶上,核心API,而無需登錄提示符 – MetaSnarf

+0

它看起來像堆棧溢出的問題已經回答正確。你幾乎肯定不想這樣做,但答案告訴你如果堅持下去該怎麼做。 – smarx