Saturday, March 3, 2012

Digest for adsense-api@googlegroups.com - 6 Messages in 2 Topics

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

    Silvano Luciani <silvano.luciani@google.com> Mar 02 10:48AM -0800  

    Hello Max,
     
    TOTAL_EARNINGS is a metric defined only for AdSense Host API users,
    for regular users it's not defined.
     
    Cheers,
    Silvano
     
    --
    Silvano Luciani | Developer Programs Engineer
     
    Google UK Limited
    Registered Office: Belgrave House, 76 Buckingham Palace Road, London
    SW1W 9TQ
    Registered in England Number: 3977902
     

     

    Sebastian Vaggi <svaggi@gmail.com> Mar 01 07:32PM -0800  

    i have this code and it returns: { "error" : "invalid_grant" }1
     
     
    $url = "https://accounts.google.com/o/oauth2/token";
    $ch = curl_init();
     
    curl_setopt($ch, CURLOPT_URL,$url);
     
    curl_setopt($ch, CURLOPT_POST, 1);
     
    $data = array('refresh_token' => '', // i put here the refresh token i
    got the last time i got the token
    'client_id' => '', //i put here my client id
    'client_secret' => '', // i put here my client secret
    'grant_type' => 'refresh_token');
     
     
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
     
    $result= curl_exec ($ch);
    curl_close ($ch);
    print $result;
     

     

    Silvano Luciani <silvano.luciani@google.com> Mar 02 02:34AM -0800  

    Hello Sebastian,
     
    can you show me the code that you're using to start the flow and
    request the access token?
     
    Cheers,
    Silvano
     
    --
    Silvano Luciani | Developer Programs Engineer
     
    Google UK Limited
    Registered Office: Belgrave House, 76 Buckingham Palace Road, London
    SW1W 9TQ
    Registered in England Number: 3977902
     

     

    Sebastian Vaggi <svaggi@gmail.com> Mar 02 05:57AM -0800  

    yes, its funny because the code i am using have your copyright :P
     
    /**
    * Include the library files for the api client and AdSense service
    class.
    */
    require_once "google-api-php-client/src/apiClient.php";
    require_once "google-api-php-client/src/contrib/
    apiAdsenseService.php";
     
    /**
    * Handles authentication and OAuth token storing.
    * Assumes the presence of a sqlite database called './
    examples.sqlite'
    * containing a table called 'auth' composed of two VARCHAR(255)
    fields called
    * 'user' and 'token'.
    *
    * @author Silvano Luciani <silvano.luciani@gmail.com>
    */
     
    class AdSenseAuth {
    protected $apiClient;
    protected $adSenseService;
    private $user;
     
    /**
    * Create the dependencies.
    * (Inject them in a real world app!!)
    */
    public function __construct() {
    // Create the apiClient instances.
    $this->apiClient = new apiClient();
    // Visit https://code.google.com/apis/console?api=adsense to
    // generate your oauth2_client_id, oauth2_client_secret, and to
    // register your oauth2_redirect_uri.
    $this->apiClient->setClientId('#################');
    $this->apiClient->setClientSecret('##################');
    $this->apiClient->setDeveloperKey('###################');
    // Point the oauth2_redirect_uri to index.php.
    $this->apiClient->setRedirectUri('http://myurl.com/tests/
    adsense_auth.php?logged');
    // Create the api AdsenseService instance.
    $this->adSenseService = new apiAdsenseService($this->apiClient);
    }
     
    /**
    * Check if a token for the user is already in the db, otherwise
    perform
    * authentication.
    * @param string $user The user to authenticate
    */
    public function authenticate($user) {
    $this->user = $user;
    // Override the scope to use the readonly one
    $this->apiClient->setScopes(
    array("https://www.googleapis.com/auth/adsense.readonly"));
    // Go get the token
    $this->apiClient->setAccessToken($this->apiClient-
    >authenticate());
    }
     
    /**
    * Return the AdsenseService instance (to be used to retrieve data).
    * @return apiAdsenseService the authenticated apiAdsenseService
    instance
    */
    public function getAdSenseService() {
    return $this->adSenseService;
    }
     
    public function getToken() {
    return $this->apiClient->getAccessToken();
    }
     
    }
     
     
    $client = new AdSenseAuth();
    $client->authenticate("john");
     
    if(isset($_GET["logged"])){
    echo $client->getToken();
    }
     
    On 2 mar, 07:34, Silvano Luciani <silvano.luci...@google.com> wrote:

     

    Silvano Luciani <silvano.luciani@google.com> Mar 02 07:35AM -0800  

    Hello Sebastian,
     
    That's strange. If you use the code from the example, AdSenseAuth and
    the Google API PHP Client take care of everything for you. I've just
    checked with my staged version of the example code and it's correctly
    refreshing the tokens.
     
    Just to understand, do you use the code from the example to request
    the token and then you use the code that you have developed to refresh
    the token?
     
    Cheers,
    Silvano
     
    --
    Silvano Luciani | Developer Programs Engineer
     
    Google UK Limited
    Registered Office: Belgrave House, 76 Buckingham Palace Road, London
    SW1W 9TQ
    Registered in England Number: 3977902
     

     

    Sebastian Vaggi <svaggi@gmail.com> Mar 02 09:48AM -0800  

    I use the last code i pasted to get the token and i also get the
    refresh token, then i try to get a new token using that refresh token
    with the code i pasted first, and i get the: { "error" :
    "invalid_grant" }1
     
    What i need to do is to have a token forever so i can access to my
    adsense account from a PHP file and get some reports with a cronjob
    that will run and get that info and keep it updated. I dont know if
    this is the right way to do it.
     
    Regards
    Sebastian
     

     

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