Re: Credentials timing out

48 views
Skip to first unread message

Jose Alcérreca (AdSense API Team)

unread,
Mar 27, 2013, 7:22:49 AM3/27/13
to adsen...@googlegroups.com
Hi,

It seems that you are not using the refresh token to generate new access tokens.

We've just created an example in Django for the AdSense Host API, but you can browse it to find how to perform the OAuth2 handshake (using client libraries).


Cheers,
Jose



---
Jose Alcérreca
Developer Relations

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


Google Inc.| Developer Relations | AdSense API Team | developers.google.com/adsense


On Tuesday, 26 March 2013 18:06:03 UTC, Dustin Davis wrote:
I've set up a python/django project to import our daily revenue from adsense. I have it working except for the fact that I have to do the Oauth2 handshake everyday or else it seems my credentials become invalid.

I blogged about how I set it up, including code samples: http://www.nerdydork.com/python-django-google-adsense-api.html

Is it possible to store long term credentials so I don't have to keep doing the oauth handshake?

Jose Alcérreca (AdSense API Team)

unread,
Mar 27, 2013, 12:10:20 PM3/27/13
to adsen...@googlegroups.com
Hi,


But I would encourage you to use the client libraries that handle all that for you.

Cheers,
Jose


---
Jose Alcérreca
Developer Relations

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


Google Inc.| Developer Relations | AdSense API Team | developers.google.com/adsense


On Wednesday, 27 March 2013 15:58:30 UTC, Dustin Davis wrote:
Sorry, what is the refresh token? Where do I find documentation on this?

Jose Alcérreca (AdSense API Team)

unread,
Mar 27, 2013, 2:01:04 PM3/27/13
to adsen...@googlegroups.com
Hi Dustin,

Please, read my previous e-mail where I posted a link to the holistic sample that uses Django.


You'll only need to create your service from your view with:

service = api_utils.initialize_service()

Cheers,
Jose



---
Jose Alcérreca
Developer Relations

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


Google Inc.| Developer Relations | AdSense API Team | developers.google.com/adsense

On Wednesday, 27 March 2013 16:30:33 UTC, Dustin Davis wrote:
I'm still confused. I'm so lost here. Sorry, this is my first time every working with Google APIs & Oath2.

Here is my django view:

import os

from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.sites.models import Site
from django.http import HttpResponseBadRequest, HttpResponse
from django.http import HttpResponseRedirect
from oauth2client import xsrfutil
from oauth2client.client import flow_from_clientsecrets
from oauth2client.django_orm import Storage

from .models import Credential


CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')

FLOW = flow_from_clientsecrets(
    CLIENT_SECRETS,
    redirect_uri='http://{0}/adsense/oauth2callback/'.format(
        Site.objects.get_current().domain))


@login_required
def index(request):
    storage = Storage(Credential, 'id', request.user, 'credential')
    credential = storage.get()
    if credential is None or credential.invalid is True:
        FLOW.params['state'] = xsrfutil.generate_token(settings.SECRET_KEY,
                                                       request.user)
        authorize_url = FLOW.step1_get_authorize_url()
        return HttpResponseRedirect(authorize_url)
    else:
        return HttpResponse('Already validated.')


@login_required
def auth_return(request):
    if not xsrfutil.validate_token(
            settings.SECRET_KEY, request.REQUEST['state'], request.user):
        return  HttpResponseBadRequest()
    credential = FLOW.step2_exchange(request.REQUEST)
    storage = Storage(Credential, 'id', request.user, 'credential')
    storage.put(credential)
    return HttpResponseRedirect("/")

What exactly am I missing? In auth_return I noticed that credential.refresh_token is None. Should the credential contain a refresh_token?

Jose Alcérreca (AdSense API Team)

unread,
Mar 27, 2013, 4:38:10 PM3/27/13
to adsen...@googlegroups.com
Hey Dustin,

Just start by making sure this works


It's the Python sample for the AdSense Management API. It's really easy to set up so let's start from there.

Cheers,
Jose

---
Jose Alcérreca
Developer Relations

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


Google Inc.| Developer Relations | AdSense API Team | developers.google.com/adsense


On Wednesday, 27 March 2013 19:57:46 UTC, Dustin Davis wrote:
I think I found the problem. I set the 'approval_prompt' param to 'force' and now I get a refresh token returned. I guess I will know for sure tomorrow if this solved the problem.

@login_required
def index(request):
    storage = Storage(Credential, 'id', request.user, 'credential')
    credential = storage.get()
    if credential is None or credential.invalid is True:
        FLOW.params['state'] = xsrfutil.generate_token(settings.SECRET_KEY,
                                                       request.user)
        FLOW.params['approval_prompt'] = 'force'
        authorize_url = FLOW.step1_get_authorize_url()
        return HttpResponseRedirect(authorize_url)
    else:
        return HttpResponse('Validated.')
Reply all
Reply to author
Forward
0 new messages