How to refresh token via curl?

892 views
Skip to first unread message

nema vashe

unread,
Jun 25, 2020, 6:41:47 AM6/25/20
to AdMob API Developer Forum
Hi there. I`m trying to use curl to get reports, like in this instruction: https://developers.google.com/admob/api/v1/getting-started#curl-command-line
Everithing was good, but access token expired after few minutes, and i really dont know why you made so small lifetime...
So, how can i refresh token via curl?

Maksym Prokhorenko

unread,
Jun 25, 2020, 2:42:51 PM6/25/20
to AdMob API Developer Forum
Thanks for your question.

I would split the answer into two independent q&a sections:
  Q: What is access token lifetime?
  A: Lifetime is pretty standard across the APIs (60 minutes); 

  Q: How to refresh the access token?
  A: You can get new token with the same oauth2l command used in the curl example
       I.e, oauth2l header --json path_to_credentials_json https://www.googleapis.com/auth/admob.report
             oauth2l fetch --json path_to_credentials_json https://www.googleapis.com/auth/admob.report
      The oauth2l stores refresh token in your home directory and use it each time you request an access one.
      For different programmatic approaches, you can take a look into oauth2 documentation.

Many Thanks

nema vashe

unread,
Jul 2, 2020, 7:01:52 AM7/2/20
to AdMob API Developer Forum
So, i dont understand some things.
What i did:
1. Go to settings on my ADMOB account and get my 'pub-XXXXXXXXXX'
2. Go to  https://developers.google.com/oauthplayground/  and authorize Admob api via url in step 1. Then, on step 2 i get access and refresh tokens.

So, now i can get report like this:
$content = <<<EOF
{
  "report_spec": {
    "date_range": {
      "start_date": {"year": 2020, "month": 6, "day": 20},
      "end_date": {"year": 2020, "month": 6, "day": 30}
    },
    "dimensions": ["DATE"],
    "metrics": ["CLICKS", "AD_REQUESTS", "IMPRESSIONS", "ESTIMATED_EARNINGS"],
    "dimension_filters": [{"dimension": "COUNTRY", "matches_any": {"values": ["US"]}}],
    "sort_conditions": [{"metric":"CLICKS", order: "DESCENDING"}],
    "localization_settings": {"currency_code": "USD", "language_code": "en-US"}
  }
}
EOF;
$auth_token = 'my_auth_token_from_step_2';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $auth_token , "Content-Type: application/json" ));
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $result = curl_exec($ch);
        curl_close($ch);
var_dump($result);

And it works only 3600 seconds.
In your reply you said, that i need credentials_json, but i dont have it!
I explore all dev console, but really didn`t find how to generate it.

Can toy just give a example on php, how to use only refresh token, not json files? Cause documentation is really bad...
четверг, 25 июня 2020 г. в 21:42:51 UTC+3, Maksym Prokhorenko:

Maksym Prokhorenko

unread,
Jul 6, 2020, 4:09:33 PM7/6/20
to AdMob API Developer Forum
Let me split the answer into two sections the first one helps with the credentials JSON file and the second shares the existing PHP examples.

How to find/create credentials_json
I would suggest to take a look into the AdMob API basics:
Also, the "Setting up OAuth 2.0" help page has a step-by-step instruction to create credentials json.

PHP examples
In my understanding, it is impossible to use a refresh token only, because OAuth requires client id (not a publisher) and secret/password, which is presented in the secret/credential json, or could be found/created on the GCP credential page.

Could you please share the most confusing documentation part with us? It would help to make materials clearer to others.

Many Thanks




 
Reply all
Reply to author
Forward
0 new messages