Can not connect to Google Adsense API: Fatal error: Uncaught exception 'Google_Service_Exception'

101 views
Skip to first unread message

Khoa Do Dinh

unread,
Jan 12, 2020, 9:38:06 PM1/12/20
to AdSense API Forum
Hi all,

We using (PHP code) to connect Google Adsense API:

$cred = __DIR__ .'/credentials.json'; (this file download when we register an account and enable Adsense API)
$client = new Google_Client();

$client->setAuthConfig($cred);
$client->setAccessType( "offline" );
$client->setApprovalPrompt('force');

$service = new Google_Service_AdSense($client);

But we recevei a message:

Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Login Required" } } ' in /home/dashboard.net/public_html/adsense/vendor/google/apiclient/src/Google/Http/REST.php:118 Stack trace: #0 /home/dashboard.net/public_html/adsense/vendor/google/apiclient/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 /home/dashboard.net/public_html/adsense/vendor/google/apiclient/src/Google/Task/Runner.php(181): call_user_func_array(Array, Array) #3 /home/dashboard.net/public_html/adsense/vendor/g in /home/dashboard.net/public_html/adsense/vendor/google/apiclient/src/Google/Http/REST.php on line 118

Please let me know my error.

Thanks for your support!

Ad Sense API

unread,
Jan 13, 2020, 10:43:31 AM1/13/20
to adsen...@googlegroups.com
Hi,

I'll be able to help you with this issue. You can take a look at our PHP example code to help you get started. Per the README, you'll need to update your credentials file with the client ID, client secret, and redirect URL for OAuth to work properly. Since your error says "Login Required", my suspicion is that the redirect URL is not set to properly handle the OAuth flow. You'll need to enter the credentials of a user that has access to your AdSense account to complete the OAuth flow and make requests to the AdSense API.

Can you let me know if you've tried those steps?

Thanks,
Donovan McMurray, Ad Manager API Team

ref:_00D1U1174p._5001USwFmx:ref

Khoa Do Dinh

unread,
Jan 17, 2020, 10:08:24 AM1/17/20
to AdSense API Forum
Hi Donovan McMurray,

Thanks for your support!

When i've tried your guide, but i've received the same message.

Could you help me: how to get access_token and refresh_token for Adsense API?

Thanks!

Vào 22:43:31 UTC+7 Thứ Hai, ngày 13 tháng 1 năm 2020, asapiforumadvisor đã viết:

Ad Sense API

unread,
Jan 17, 2020, 10:27:28 AM1/17/20
to adsen...@googlegroups.com
Hi,

If you are using the PHP Client Library, you can follow the OAuth steps in the README.

Also, if you are stuck getting the refresh token and access token, you could try the OAuth Playground to authenticate with your user and get the tokens to check that your user has the access you expect. In the OAuth Playground, you basically do the same steps in the README above manually.

Let me know if you still get stuck.
Reply all
Reply to author
Forward
0 new messages