You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

FSUInnovation's avatar
FSUInnovation
Explorer | Level 4
5 years ago

header array invalid request for token auth php

I have been trying to get the header array for my php curl working. This is the structure of it.

$http_headers = array(
	"Authorization: " . base64_encode($app_key . ":" . $app_secret),
	"Content-Type: application/x-www-form-urlencoded",
	"Dropbox-API-Arg: " . json_encode(array(
	'code' => $code,
	'grant_type' => 'authorization_code',
	'redirect_url' => 'http://localhost:8080/FSUInnovation/ResumeUpload.php'	
)));

I get this error:

{"error_description": "No auth function available for given request", "error": "invalid_request"}400

I don't know what particular syntax issue is causing a problem for this particular request. I tried to base it off of my curl for file upload and download.

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

    You should send them as POST parameters, encoded using "application/x-www-form-urlencoded", not headers. Here's a basic example I just put together for calling /oauth2/token using curl in PHP:

    $app_key = "<APP_KEY>";
    $app_secret = "<APP_SECRET>";
    
    $headers = array("Authorization: Basic " . base64_encode($app_key . ":" . $app_secret),
                     "Content-Type: application/x-www-form-urlencoded");
    
    $authorization_code = "<AUTHORIZATION_CODE>";
    $redirect_uri = "<REDIRECT_URI>";
    
    $params = array("code" => $authorization_code,
                    "grant_type" => "authorization_code",
                    "redirect_uri" => $redirect_uri);
    
    $ch = curl_init('https://api.dropboxapi.com/oauth2/token');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    
    echo $response;
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    
    curl_close($ch);
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    When using "Basic" authorization, your "Authorization" header value should start with the string "Basic ". So, that line should look like:

     

    	"Authorization: Basic " . base64_encode($app_key . ":" . $app_secret),
    

    Also, note that the /oauth2/token endpoint is based on the OAuth spec and doesn't work like the other Dropbox API endpoints. Specifically, it doesn't take a "Dropbox-API-Arg" header. You should be specifiy those parameters as 'application/x-www-form-urlencoded' POST parameters, not JSON in a header or body.

     

    • FSUInnovation's avatar
      FSUInnovation
      Explorer | Level 4

      Do the other parameters need to be in a assosiative array appended to the Content Type element like I would do with the API Args, or do I need to post those parameters like this: "code: " . $code, ... ?

      • Greg-DB's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox Staff

        You should send them as POST parameters, encoded using "application/x-www-form-urlencoded", not headers. Here's a basic example I just put together for calling /oauth2/token using curl in PHP:

        $app_key = "<APP_KEY>";
        $app_secret = "<APP_SECRET>";
        
        $headers = array("Authorization: Basic " . base64_encode($app_key . ":" . $app_secret),
                         "Content-Type: application/x-www-form-urlencoded");
        
        $authorization_code = "<AUTHORIZATION_CODE>";
        $redirect_uri = "<REDIRECT_URI>";
        
        $params = array("code" => $authorization_code,
                        "grant_type" => "authorization_code",
                        "redirect_uri" => $redirect_uri);
        
        $ch = curl_init('https://api.dropboxapi.com/oauth2/token');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        
        echo $response;
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
        
        curl_close($ch);

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,882 PostsLatest Activity: 7 hours ago
325 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!