Search code examples
phpcurloauth-2.0uber-api

Why do I keep getting unsupported_grant_type when using oAuth2 and curl?


I am trying to authenticate my self with uber rush api, and I keep getting an unsupported_grant_type error message. I am not sure what am I doing wrong here. Any help would be really appreciated. Below is the code I am using

Here is what the command line request looks like:

curl -F "client_secret=<CLIENT_SECRET>" \
    -F "client_id=<CLIENT_ID>" \
    -F "grant_type=client_credentials" \
    -F "scope=delivery" \
    https://login.uber.com/oauth/v2/token

Here is how I wrote it in PHP

$cl = curl_init("https://login.uber.com/oauth/v2/token");


curl_setopt($cl,CURLOPT_POST,["client_secret"=>"********"]);
curl_setopt($cl,CURLOPT_POST,["client_id"=>"**********"]);
curl_setopt($cl,CURLOPT_POST,["grant_type"=>"client_credentials"]);
curl_setopt($cl,CURLOPT_POST,["scope"=>"delivery"]);
$content = curl_exec($cl);
curl_close($cl);


var_dump($content);

Solution

  • This is not how to add POST data to cURL. There is PHP documentation to tell you what these options actually mean. Try this instead:

    <?php
    $postdata = [
        "client_secret"=>"xxx",
        "client_id"=>"xxx",
        "grant_type"=>"client_credentials",
        "scope"=>"delivery",
    ];
    $cl = curl_init("https://login.uber.com/oauth/v2/token");
    
    curl_setopt_array($cl, [
        CURLOPT_POST           => true,
        CURLOPT_POSTFIELDS     => $postdata,
        CURLOPT_RETURNTRANSFER => true
    ]);
    $content = curl_exec($cl);
    curl_close($cl);
    var_dump($content);