2

I used OAuth for Dropbox to get access token: https://blogs.dropbox.com/developers/2012/07/using-oauth-1-0-with-the-plaintext-signature-method/

But I got error message: Warning: file_get_contents(https://api.dropbox.com/1/oauth/request_token): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

My PHP Code:

$Header = json_encode(array('Authorization: OAuth oauth_version="1.0"', "oauth_signature_method" => "PLAINTEXT", "oauth_consumer_key" => "XX", "oauth_signature" => "XX"));

$Options = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => $Header,
    )
);


$Context  = stream_context_create($Options);
$Result = file_get_contents("https://api.dropbox.com/1/oauth/request_token", false, $Context);

print_r($Result);
user3492381
  • 167
  • 9
  • Try to go for CURL. check this out: http://stackoverflow.com/a/8543512/4388034 – Arsh Singh Nov 23 '15 at 21:25
  • also check this link it will explain you way better the usage : http://fabi.me/en/php-projects/dropphp-dropbox-api-client/comment-page-2/ – Arsh Singh Nov 23 '15 at 21:30

2 Answers2

1

Based on the documentation at http://php.net/manual/en/context.http.php, it looks like the header option is just a normal string, not json_encoded. Alternatively, you should be able to use a numerically indexed array of headers, like so:

$Header = array(
"Authorization: OAuth oauth_version=\"1.0\", oauth_signature_method=\"PLAINTEXT\", oauth_consumer_key=\"XXXXX\", oauth_signature=\"XXXXX&\"\r\n"
);

$Options = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => $Header,
    )
);
kunruh
  • 874
  • 1
  • 8
  • 17
  • Hm, try this for the header instead: `$header = 'Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="", oauth_signature="&"'` (All one long string, not in an array.) – kunruh Nov 23 '15 at 21:44
  • Mr. kunruh, I tried your code, but not working with me :( – user3492381 Nov 23 '15 at 21:46
  • 1
    This is the only other guess I have: `$header = "Authorization: OAuth oauth_version=\"1.0\", oauth_signature_method=\"PLAINTEXT\", oauth_consumer_key=\"\", oauth_signature=\"&\"\r\n"`. Also make sure you are replacing the `` and `` with your correct credentials. – kunruh Nov 23 '15 at 21:52
0

This is wrong:

$Header = json_encode(array('Authorization: OAuth oauth_version="1.0"', "oauth_signature_method" => "PLAINTEXT", "oauth_consumer_key" => "XX", "oauth_signature" => "XX"));

The header paramter in an http stream is either a simple single string containing one single header key: value, or an array of key: value strings. You're stuffing in a single json string as your header, meaning it's NOT a valid http header.

Remove the json_encode() part completely. $Header = array(...) is all you need.

Marc B
  • 356,200
  • 43
  • 426
  • 500