Error Invalid Grant w/ refresh token

430 views
Skip to first unread message

Eric Haskins

unread,
Mar 22, 2012, 12:14:23 PM3/22/12
to AdSense API Forum
Ok so I get an access token and a Refresh token

I am writing the code so my server can get a new access token if it
expires (terrible for Server to Server but I guess google doesnt think
so)

$data = array(
'client_id' => 'XXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com',
'client_secret' => 'XXXXXXXXXXXXXXXXXXX',
'refresh_token' => 'XXXXXXXXXXXXXXXXXXX',
'grant_type' => 'refresh_token'
);

$ch = curl_init("https://accounts.google.com/o/oauth2/token");
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$resp = curl_exec($ch);

print_r(json_decode($resp));

RESULT:
[error] => invalid_grant


Scopes were
'https://www.googleapis.com/auth/adsense',
'https://www.googleapis.com/auth/adsense.readonly',

The structure is right out of the Documentation. Is there anyone here
that has managed to access the AdSense Management API with PHP without
using a web app i.e Server to Server with OAuth2 ??? I can do it all
day long with Twitter but for some reason but google's version seems
be all sorts of difficult.


Thx
Eric

Ludo

unread,
Apr 23, 2012, 12:19:23 PM4/23/12
to AdSense API Forum
Hello,

Same problem here !

Bye.

Sérgio Gomes

unread,
May 1, 2012, 1:10:06 PM5/1/12
to adsen...@googlegroups.com
Hello there!

Just to confirm, what sort of authentication are you using? Is it Web Server authentication, i.e. the one described in https://developers.google.com/accounts/docs/OAuth2WebServer ?

Cheers,
Sérgio

---
Sérgio Gomes
Developer Relations

Google UK Limited
Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W 9TQ
Registered in England Number: 3977902

---

Eric Haskins

unread,
Apr 30, 2012, 1:16:31 PM4/30/12
to adsen...@googlegroups.com
Ludo Try this

<?php
require_once '/www/phpinclude/google_client_api/src/apiClient.php';

$data = array(
'client_id' => 'xxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com',
'client_secret' => 'XXXXXXXXXXXXXXXXXX',
'refresh_token' => XXXXXXXXXXXXXXXXXXXXXA85zRh8',
'grant_type' => 'refresh_token'
);

$ch = curl_init("https://accounts.google.com/o/oauth2/token");

curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($data));
curl_setopt($ch, CURLINFO_HEADER_OUT, true);


$resp = curl_exec($ch);
curl_close($ch);

$json = json_decode($resp,true);

echo "Access Token: " . $json['access_token'];
--
You received this message because you are subscribed to the Google Groups
"AdSense API Forum" group.
To post to this group, send email to adsen...@googlegroups.com.
To unsubscribe from this group, send email to
adsense-api...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/adsense-api?hl=en.


Ludovic VUE

unread,
May 1, 2012, 1:14:11 PM5/1/12
to adsen...@googlegroups.com
Hello !

I did another test and it works fine !

And yes i use this sort of authentification (OAuth2).

Thanks for asking !

2012/5/1 Sérgio Gomes <sergio...@google.com>

--
You received this message because you are subscribed to the Google Groups "AdSense API Forum" group.
To view this discussion on the web visit https://groups.google.com/d/msg/adsense-api/-/lKwe1F1dSr4J.

To post to this group, send email to adsen...@googlegroups.com.
To unsubscribe from this group, send email to adsense-api...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/adsense-api?hl=en.



--
Ludovic VUE
EPITECH - Promo 2014
Secrétaire général et Webmaster de Soul Of Sound
06 07 29 63 27

Reply all
Reply to author
Forward
0 new messages