Thursday, March 28, 2013

Digest for adsense-api@googlegroups.com - 9 Messages in 1 Topic

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

    Dustin Davis <dustin.davis@gmail.com> Mar 26 11:06AM -0700  

    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)" <adsenseapiadvisor+jose@google.com> Mar 27 04:22AM -0700  

    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).
     
    https://code.google.com/p/adsense-apis-showcase/source/browse/#git%2FAdSenseHostHolisticSample
     
    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:

     

    Dustin Davis <dustin.davis@gmail.com> Mar 27 08:58AM -0700  

    Sorry, what is the refresh token? Where do I find documentation on this?
     
    On Wednesday, March 27, 2013 5:22:49 AM UTC-6, Jose Alcérreca (AdSense API
    Team) wrote:

     

    "Jose Alcérreca (AdSense API Team)" <adsenseapiadvisor+jose@google.com> Mar 27 09:10AM -0700  

    Hi,
     
    https://developers.google.com/adsense/management/tutorials/oauth-generic
     
    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:

     

    Dustin Davis <dustin.davis@gmail.com> Mar 27 09:30AM -0700  

    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,
    scope='https://www.googleapis.com/auth/adsense.readonly',
    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?
     
     
     
    On Wednesday, March 27, 2013 10:10:20 AM UTC-6, Jose Alcérreca (AdSense API
    Team) wrote:

     

    "Jose Alcérreca (AdSense API Team)" <adsenseapiadvisor+jose@google.com> Mar 27 11:01AM -0700  

    Hi Dustin,
     
    Please, read my previous e-mail where I posted a link to the holistic
    sample that uses Django.
     
    https://code.google.com/p/adsense-apis-showcase/source/browse/AdSenseHostHolisticSample/api_utils.py
     
    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:

     

    Dustin Davis <dustin.davis@gmail.com> Mar 27 12:57PM -0700  

    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.')

     

    Dustin Davis <dustin.davis@gmail.com> Mar 27 11:52AM -0700  

    I've tried using a similar method but this example uses some kind of
    command line interface and Google wants to open localhost:8080 which
    doesn't exist. Do I need to create a command line app to run once just to
    get a credential file (adsense.dat). If I do that, will that credential
    file last indefinitely?
     
    Here is the task where I'm running the report:
     
    import datetime
    import httplib2
     
    from apiclient.discovery import build
    from celery.task import PeriodicTask
    from django.contrib.auth.models import User
    from oauth2client.django_orm import Storage
     
    from .models import Credential, Revenue
     
     
    TODAY = datetime.date.today()
    YESTERDAY = TODAY - datetime.timedelta(days=1)
     
     
    class GetReportTask(PeriodicTask):
    run_every = datetime.timedelta(minutes=2)
     
    def run(self, *args, **kwargs):
    scraper = Scraper()
    scraper.get_report()
     
     
    class Scraper(object):
    def get_report(self, start_date=YESTERDAY, end_date=TODAY):
    user = User.objects.get(pk=1)
    storage = Storage(Credential, 'id', user, 'credential')
    credential = storage.get()
    if not credential is None or credential.invalid is False:
    http = httplib2.Http()
    http = credential.authorize(http)
    service = build('adsense', 'v1.2', http=http)
    reports = service.reports()
    report = reports.generate(
    startDate=start_date.strftime('%Y-%m-%d'),
    endDate=end_date.strftime('%Y-%m-%d'),
    dimension='DATE',
    metric='EARNINGS',
    )
    data = report.execute()
    for row in data['rows']:
    date = row[0]
    revenue = row[1]
     
    try:
    record = Revenue.objects.get(date=date)
    except Revenue.DoesNotExist:
    record = Revenue()
    record.date = date
    record.revenue = revenue
    record.save()
    else:
    print 'Invalid Adsense Credentials :('
     
    Am I not essentially doing the same thing as the
    api_utils.initilize_service() call. It seems credentials is just being
    stored in the database rather than a file. But what is missing? Why am I
    not getting the refresh_token?
     
    Looking at the database, this is what is stored for the credential:
     
    <oauth2client.client.OAuth2Credentials object at 0x10c270210>
     
    Is that a problem? Should it be some kind of data?
     
     
    On Wednesday, March 27, 2013 12:01:04 PM UTC-6, Jose Alcérreca (AdSense API
    Team) wrote:

     

    "Jose Alcérreca (AdSense API Team)" <adsenseapiadvisor+jose@google.com> Mar 27 01:38PM -0700  

    Hey Dustin,
     
    Just start by making sure this works
     
    https://code.google.com/p/google-api-python-client/source/browse/#hg%2Fsamples%2Fadsense
     
    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:

     

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 unsubscribe from this group and stop receiving emails from it, send an email to adsense-api+unsubscribe@googlegroups.com.
To post to this group, send email to adsense-api@googlegroups.com.
Visit this group at http://groups.google.com/group/adsense-api?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

1 comment:

  1. 14 scRnd 7 this rnd will shape point of tail: sexcam Sc
    in next sc, sc in each free loop around, join in next sc repeat around.


    My weblog: sex cam

    ReplyDelete