We are aware of the issue with the badge emails resending to everyone, we apologise for the inconvenience - learn more here.

Forum Discussion

pineapps's avatar
pineapps
Helpful | Level 5
7 years ago

OAuth2 access token request - Basic authentication not working

I am trying to integrate Dropbox in my webservice. However I decided to use a direct integration instead of any APIs as I need a limited number of functions only and not used to code object orientated in PHP.

 

For any reason my authentication token handling is not working. I figured out, something is wrong with the basic authentifiation, as some other functions which should work with basic as well as bearer authentification do only work with the last one mentioned.

 

function dropboxAuthentication($code){

    global $authToken;
$parameters['code']=$code;
$parameters['grant_type']="authorization_code"; $parameters['redirect_uri']="https://myurl.com/file.php"; // Create the context for the request $context = stream_context_create(array( 'http' => array( 'method' => 'POST', 'header' => 'Authorization: {Basic '.base64_encode("my app id:my app secret").'}\r\n'. 'Content-Type: application/json\r\n', 'content' => json_encode($parameters) ) )); // Send the request $response = file_get_contents('https://api.dropboxapi.com/oauth2/token', FALSE, $context); // Check for errors if($response === FALSE){ die('Error'); } // Decode the response $result=json_decode($response, TRUE); return $result['code']; }

 

The file accessed after the login calls the function with the temporary code given. The temporary code is received well.

 

The result is always 

Error

I checked the response code which is a HTTP Error 400.

 

 

Thank you very much for your support!

  • Greg-DB's avatar
    Greg-DB
    7 years ago

    Thanks! The API is indicating that the request wasn't valid. This particular error message isn't too specific, but it seems to be indicating that the necessary information wasn't given.

     

    Looking through your code, you appear to be sending the parameters as JSON. The /oauth2/token endpoint expects application/x-www-form-urlencoded POST parameters though (with the app key and secret in particular optionally sent in the Authorization header instead).

     

    Please update your code to send the rest of the parameters as application/x-www-form-urlencoded POST parameters instead.

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff
    What is the content of the body for 400 HTTP response? It should contain a more useful error message.
    • pineapps's avatar
      pineapps
      Helpful | Level 5

      Greg K. schrieb:
      What is the content of the body for 400 HTTP response? It should contain a more useful error message.

      For me however, this is not really useful:

       

      I did a 

      echo implode($http_response_header);

       

      Returns

      HTTP/1.1 400 Bad RequestServer: nginxDate: Fri, 05 Jan 2018 20:33:52 GMTContent-Type: application/jsonConnection: closeX-Dropbox-Request-Id: 864d06461b2bc9f132a4cfa44ca9f2a5X-Frame-Options: DENYContent-Security-Policy: sandbox; frame-ancestors 'none'X-Content-Type-Options: nosniffContent-Disposition: attachment; filename='error'

      Maybe, this is more usefull for you :) Thank you in advance!

      • Greg-DB's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox Staff
        That appears to be the response headers only. Can you print the response body?

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,877 PostsLatest Activity: 12 months ago
326 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!