<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic OAuth2 access token request - Basic authentication not working in Dropbox API Support &amp; Feedback</title>
    <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/OAuth2-access-token-request-Basic-authentication-not-working/m-p/259164#M15054</link>
    <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;function dropboxAuthentication($code){

    global $authToken;&lt;BR /&gt;    $parameters['code']=$code;&lt;BR /&gt;    $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' =&amp;gt; array(
            'method' =&amp;gt; 'POST',
            'header' =&amp;gt; 'Authorization: {Basic '.base64_encode("my app id:my app secret").'}\r\n'.
                'Content-Type: application/json\r\n',
            'content' =&amp;gt; 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'];

}&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The file accessed after the login calls the function with the temporary code given. The temporary code is received well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The result is always&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Error&lt;/PRE&gt;
&lt;P&gt;I checked the response code which is a HTTP Error 400.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much for your support!&lt;/P&gt;</description>
    <pubDate>Wed, 29 May 2019 09:16:19 GMT</pubDate>
    <dc:creator>pineapps</dc:creator>
    <dc:date>2019-05-29T09:16:19Z</dc:date>
    <item>
      <title>OAuth2 access token request - Basic authentication not working</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/OAuth2-access-token-request-Basic-authentication-not-working/m-p/259164#M15054</link>
      <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;function dropboxAuthentication($code){

    global $authToken;&lt;BR /&gt;    $parameters['code']=$code;&lt;BR /&gt;    $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' =&amp;gt; array(
            'method' =&amp;gt; 'POST',
            'header' =&amp;gt; 'Authorization: {Basic '.base64_encode("my app id:my app secret").'}\r\n'.
                'Content-Type: application/json\r\n',
            'content' =&amp;gt; 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'];

}&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The file accessed after the login calls the function with the temporary code given. The temporary code is received well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The result is always&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Error&lt;/PRE&gt;
&lt;P&gt;I checked the response code which is a HTTP Error 400.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much for your support!&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2019 09:16:19 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/OAuth2-access-token-request-Basic-authentication-not-working/m-p/259164#M15054</guid>
      <dc:creator>pineapps</dc:creator>
      <dc:date>2019-05-29T09:16:19Z</dc:date>
    </item>
    <item>
      <title>Re: OAuth 2 access token request - basic authentication not working</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/OAuth2-access-token-request-Basic-authentication-not-working/m-p/259167#M15055</link>
      <description>What is the content of the body for 400 HTTP response? It should contain a more useful error message.</description>
      <pubDate>Fri, 05 Jan 2018 20:29:47 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/OAuth2-access-token-request-Basic-authentication-not-working/m-p/259167#M15055</guid>
      <dc:creator>Greg-DB</dc:creator>
      <dc:date>2018-01-05T20:29:47Z</dc:date>
    </item>
    <item>
      <title>Re: OAuth 2 access token request - basic authentication not working</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/OAuth2-access-token-request-Basic-authentication-not-working/m-p/259170#M15057</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;Greg K. schrieb:&lt;BR /&gt;What is the content of the body for 400 HTTP response? It should contain a more useful error message.&lt;/BLOCKQUOTE&gt;&lt;P&gt;For me however, this is not really useful:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did a&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;echo implode($http_response_header);&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Returns&lt;/P&gt;&lt;PRE&gt;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'&lt;/PRE&gt;&lt;P&gt;Maybe, this is more usefull for you &lt;img class="lia-deferred-image lia-image-emoji" src="https://www.dropboxforum.com/html/@FBF7D2AB59A0D6E861EBF6A36F93B7E2/emoticons/1f642.png" alt=":slightly_smiling_face:" title=":slightly_smiling_face:" /&gt; Thank you in advance!&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jan 2018 20:38:20 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/OAuth2-access-token-request-Basic-authentication-not-working/m-p/259170#M15057</guid>
      <dc:creator>pineapps</dc:creator>
      <dc:date>2018-01-05T20:38:20Z</dc:date>
    </item>
    <item>
      <title>Re: OAuth 2 access token request - basic authentication not working</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/OAuth2-access-token-request-Basic-authentication-not-working/m-p/259171#M15058</link>
      <description>That appears to be the response headers only. Can you print the response body?</description>
      <pubDate>Fri, 05 Jan 2018 20:45:18 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/OAuth2-access-token-request-Basic-authentication-not-working/m-p/259171#M15058</guid>
      <dc:creator>Greg-DB</dc:creator>
      <dc:date>2018-01-05T20:45:18Z</dc:date>
    </item>
    <item>
      <title>Re: OAuth 2 access token request - basic authentication not working</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/OAuth2-access-token-request-Basic-authentication-not-working/m-p/259174#M15060</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;Greg K. schrieb:&lt;BR /&gt;That appears to be the response headers only. Can you print the response body?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I managed to get additional information from the HTTP request:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;{"url":"https:\/\/api.dropboxapi.com\/oauth2\/token","content_type":"application\/json","http_code":400,"header_size":385,"request_size":350,"filetime":-1,"ssl_verify_result":0,"redirect_count":0,"total_time":0.287188,"namelookup_time":0.004172,"connect_time":0.018384,"pretransfer_time":0.053378,"size_upload":23,"size_download":97,"speed_download":337,"speed_upload":80,"download_content_length":-1,"upload_content_length":23,"starttransfer_time":0.287049,"redirect_time":0,"redirect_url":"","primary_ip":"162.125.66.7","certinfo":[],"primary_port":443,"local_ip":"85.13.153.199","local_port":45981}&lt;/PRE&gt;
&lt;P&gt;Last data received&lt;/P&gt;
&lt;PRE&gt;{"error_description": "No auth function available for given request", "error": "invalid_request"}&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jan 2018 21:31:31 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/OAuth2-access-token-request-Basic-authentication-not-working/m-p/259174#M15060</guid>
      <dc:creator>pineapps</dc:creator>
      <dc:date>2018-01-05T21:31:31Z</dc:date>
    </item>
    <item>
      <title>Re: OAuth 2 access token request - basic authentication not working</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/OAuth2-access-token-request-Basic-authentication-not-working/m-p/259182#M15064</link>
      <description>&lt;P&gt;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&amp;nbsp;information wasn't given.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looking through your code, you appear to be sending the parameters as JSON. The &lt;A href="https://www.dropbox.com/developers/documentation/http/documentation#oauth2-token" target="_self"&gt;/oauth2/token&lt;/A&gt;&amp;nbsp;endpoint expects&amp;nbsp;application/x-www-form-urlencoded POST parameters though (with the app key and secret in particular optionally sent in the Authorization header instead).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please update your code to send the rest of the parameters as&amp;nbsp;application/x-www-form-urlencoded POST parameters instead.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jan 2018 21:36:11 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/OAuth2-access-token-request-Basic-authentication-not-working/m-p/259182#M15064</guid>
      <dc:creator>Greg-DB</dc:creator>
      <dc:date>2018-01-05T21:36:11Z</dc:date>
    </item>
    <item>
      <title>Re: OAuth 2 access token request - basic authentication not working</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/OAuth2-access-token-request-Basic-authentication-not-working/m-p/259190#M15065</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;Greg K. schrieb:&lt;BR /&gt;
&lt;P&gt;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&amp;nbsp;information wasn't given.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looking through your code, you appear to be sending the parameters as JSON. The &lt;A href="https://www.dropbox.com/developers/documentation/http/documentation#oauth2-token" target="_self"&gt;/oauth2/token&lt;/A&gt;&amp;nbsp;endpoint expects&amp;nbsp;application/x-www-form-urlencoded POST parameters though (with the app key and secret in particular optionally sent in the Authorization header instead).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please update your code to send the rest of the parameters as&amp;nbsp;application/x-www-form-urlencoded POST parameters instead.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This helps me very much!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried to fix this with a simple but dirty solution&lt;/P&gt;
&lt;PRE&gt;$response = file_get_contents('https://my_app_id:my_app_key@api.dropboxapi.com/oauth2/token', FALSE, http_build_query($parameters));&lt;/PRE&gt;
&lt;P&gt;Which seems to be almost working (at least detects if I am missing parameters, so the authentication seems to work).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The result of the function http_build_query is&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;code=authcodegoeshere&amp;amp;grant_type=authorization_code&amp;amp;redirect_uri=https%3A%2F%2Fmydomain.com%2Ffile.php&lt;/PRE&gt;
&lt;P&gt;and should be in the correct format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The updated error says this:&lt;/P&gt;
&lt;PRE&gt;:{"error_description": "unknown field \"var1\"", "error": "invalid_request"}&lt;/PRE&gt;
&lt;P&gt;Thank you very much for your help!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: Got it! Created a new complex function accessing the data with curl. Thank you very much for your help, this helped me very much!&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jan 2018 22:28:25 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/OAuth2-access-token-request-Basic-authentication-not-working/m-p/259190#M15065</guid>
      <dc:creator>pineapps</dc:creator>
      <dc:date>2018-01-05T22:28:25Z</dc:date>
    </item>
  </channel>
</rss>

