Bit.ly Access Token INVALID_CLIENT_ID

So I'm trying to retrieve an access_token from bit.ly to use their url shorten API call. Everytime I make the curl call I get INVALID_CLIENT_ID as a response. I quadruple checked, all my keys are fine. What could be happening?

Here is the code:

define('BITLY_CLIENT_ID','xxx');
define('BITLY_CLIENT_SECRET','xxx');
define('BITLY_API_KEY','xxx');

function curl($url, $params) {

    $params_str = http_build_query($params);
    $curl = curl_init();
    $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
    $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: "; // browsers keep this blank.

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_POST,count($params));
    curl_setopt($curl, CURLOPT_POSTFIELDS,urlencode($params_str));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $rs = curl_exec($curl);
    curl_close($curl);
    return $rs;
}

$access_token = curl('https://api-ssl.bitly.com/oauth/access_token',array('client_id'=>BITLY_CLIENT_ID,'client_secret'=>BITLY_CLIENT_SECRET,'x_auth_username'=>'MY_USERNAME','x_auth_password'=>'MY_PASSWORD'));

echo $access_token;exit; // Returns INVALID_CLIENT_ID

Answers


From API documentation: Authentication via XAuth must be requested by e-mailing api@bitly.com

Could this be the reason?

Also, urlencoding the whole $params_str looks suspicious.


So I emailed Bit.ly earlier today (Awesome service btw, quick and polite), this is the response they gave to me:

If all you're looking to do is to get an access_token for your own account, I suggest using the basic auth mechanism documented at http://dev.bitly.com/authentication.html. Just load up terminal and curl the following URL w/ your username and password:

curl -u "username:password" -X POST "https://api-ssl.bitly.com/oauth/access_token"

So run this and copy and paste your access_token into your config.

My new curl call looked like this:

curl('https://api-ssl.bitly.com/v3/shorten',array('apiKey'=>BITLY_API_KEY,'access_token'=>BITLY_ACCESS_TOKEN,'longUrl'=>'http://www.google.com'))

P.S - urlencoding the $params_str broke the call too. Thanks timoh!


Need Your Help

Escaping curl command in Windows

windows curl marklogic

I'm trying to run a curl command from the command line in Windows, but for the life of me I can't figure out how I'm supposed to escape it.