Tuesday, March 27, 2012

Digest for adsense-api@googlegroups.com - 5 Messages in 4 Topics

Group: http://groups.google.com/group/adsense-api/topics

    "Nikolai P." <nik.peso@gmail.com> Mar 25 03:15PM -0700  

    Oh, but some examples of generating JWT do exist in docs. Here they are:
     
    https://developers.google.com/accounts/docs/OAuth2ServiceAccount
     
    So here's my PHP working code base on those rules:
     
    <?php
    $now = time();
    $jwt_header = base64_encode(json_encode(array(
    'alg' => 'RS256',
    'typ' => 'JWT'
    )));
    $jwt_claim = base64_encode(json_encode(array(
    'iss' => '268481************@developer.gserviceaccount.com',
    'scope' => 'https://www.googleapis.com/auth/adsense.readonly',
    'aud' => 'https://accounts.google.com/o/oauth2/token',
    'exp' => $now+3600,
    'iat' => $now
    )));
     
    # here i include one class from PHP client library to create JWT signature
    require_once 'google-api-php-client/src/auth/apiSigner.php';
    $p12 = new apiP12Signer('707ea88c********-privatekey.p12', 'notasecret');
    $jwt_signature = base64_encode($p12->sign($jwt_header . '.' . $jwt_claim));
     
    $c = curl_init();
    curl_setopt_array($c, array(
    CURLOPT_HEADER => false,
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => array(
    'grant_type' => 'assertion',
    'assertion_type' => 'http://oauth.net/grant_type/jwt/1.0/bearer',
    'assertion' => implode('.', array($jwt_header,$jwt_claim,$jwt_signature))
    ),
    CURLOPT_URL => 'https://accounts.google.com/o/oauth2/token'
    ));
    $x = curl_exec($c); $token = json_decode($x, true);
     
    /*
    at this point i'm authorized
    $x contains something like this:
     
    {
    "access_token" : "1/8xbJqaOZXSUZbHLl5EOtu1pxz3fmmetKx9W8CV4t79M",
    "token_type" : "Bearer",
    "expires_in" : 3600
    }
     
    so i just json_decode it and continue my requests to Adsense API:
    */
     
    $query = 'access_token=' . $token['access_token'];
    $query .= '&accountId=pub-*****';
    $query .= '&startDate=2012-03-01';
    $query .= '&endDate=' . date('Y-m-d');
    $query .=
    '&metric=PAGE_VIEWS&metric=AD_REQUESTS&metric=MATCHED_AD_REQUESTS&metric=CLICKS&metric=EARNINGS';
    $query .= '&dimension=DATE';
     
    curl_setopt_array($c, array(
    CURLOPT_HTTPGET => true,
    CURLOPT_URL => 'https://www.googleapis.com/adsense/v1.1/reports?' . $query
    ));
    $x = curl_exec($c); curl_close($c);
    ?>
     
    However i have one problem at this final step. Request ALWAYS gives me
    noAdSenseAccount reply and i have no clue how to fix it..
     
    But authorization works great anyway.
     
     
    On Friday, March 23, 2012 3:04:32 AM UTC+11, Eric Haskins wrote:
     
    > Then why even put it in your docs??? Seriously and I mean no
     
    disrespect if you say in the Docs "Here choose from these methods to

     

    Eric Haskins <eric@voodoo.com> Mar 26 01:02PM -0700  

    Nikolai,
     
    You are the man! I realized with your help of course I was putting
    in the Client ID instead of the Email Address for the Service Account
    in the iss Field which would result in invalid_grant ;-)
     
    Eric
     

     

    Eduardo Scoz <eduardoscoz@gmail.com> Mar 26 08:50AM -0700  

    Hi guys,
     
    Any plan on adding the Platform name dimension to the API? I can't find it.
    It's available in Adsense, but not from the API.
     
    Thanks!

     

    Eric Haskins <eric@voodoo.com> Mar 23 08:26AM -0700  

    $data = array(
    'client_id' =>
    'xxxxxxxx-9subht2tq6pk23jj1el05guh6jb22k77.apps.googleusercontent.com',
    'client_secret' => 'VYduBxxwaB9veXXXXXXXXXXXX',
    'refresh_token' => '1\/-PC83EyXNbNi5OULeJDQR8G5q5ZyzEJnY7QHQcg8q04',
    '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);
     
    Results in Error = invalid_grant
     
    Do I need to base64url encode these??

     

    "Raúl Mellado" <raulmellado@gmail.com> Mar 23 02:56PM -0700  

    Thanks Sérgio, this definitely helps a lot! :-)
     
     
    On Saturday, March 24, 2012 4:22:58 AM UTC+12, Sérgio Gomes wrote:
    >> this?
     
    >> Thanks a lot in advance to the great Adsense API support team!
     
    >> Raul
     
    On Saturday, March 24, 2012 4:22:58 AM UTC+12, Sérgio Gomes wrote:

     

You received this message because you are subscribed to the Google Group adsense-api.
You can post via email.
To unsubscribe from this group, send an empty message.
For more options, visit this group.

--
You received this message because you are subscribed to the Google Groups "AdSense API Forum" group.
To post to this group, send email to adsense-api@googlegroups.com.
To unsubscribe from this group, send email to adsense-api+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/adsense-api?hl=en.

No comments:

Post a Comment