goq123
goq123

Reputation: 59

Get Oauth 2.0 access token to adsense

I'm trying to get Access_token which I need to use in adsense host api to authorization my request and I have code like this

$curlHandle = curl_init();

    $data = array(
        'code' => 'AFC',
        'client_id' => 'myClientId',
        'client_secret'=> 'myClientSecret',
        'redirect_uri' => 'http://smk.dev',
        'grant_type'=>'authorization_code'
    );
    $url = 'https://accounts.google.com/o/oauth2/token';

    curl_setopt($curlHandle,CURLOPT_URL,$url);
    curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curlHandle, CURLOPT_POST, 1);
    curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);

    $result = curl_exec($curlHandle);

    $data = json_decode($result);

But i get response with error "invalid_grant" so where i am wrong? what is the code parameter? i use parameter from google adsense host api

Upvotes: 0

Views: 875

Answers (1)

Dean Lukies
Dean Lukies

Reputation: 109

It's a multi-step process

  • First you need to get an Auth code (The 'code' you are missing - you need to make a curl request for this)
  • Then you use that code to get a token (This is your curl request above)
  • Then you use that token to call the API

Take a look at this (Mainly the first picture) https://developers.google.com/identity/protocols/OAuth2

Upvotes: 0

Related Questions