question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Service account access_token is None

See original GitHub issue

Hey,

I’m using the google-api-python-client==1.4.2 and oauth2client==1.5.1, with service accounts in our internal SEO tool, which communicates with the webmaster tools API.

As I’m trying to batch fix crawl error samples, I get the following error:

Traceback (most recent call last):
  File "/home/seodev/project/venv/lib/python3.4/site-packages/django/core/handlers/base.py", line 132, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/seodev/project/venv/lib/python3.4/site-packages/django/views/decorators/http.py", line 45, in inner
    return func(request, *args, **kwargs)
  File "/home/seodev/project/venv/lib/python3.4/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "./visualizer/seo/views.py", line 339, in json_sample_markfixed_many
    result_json = cese_markfixed_many(request, id_list)
  File "./visualizer/seo/utils.py", line 571, in cese_markfixed_many
    exc_callback_item=exc_item, exc_callback_batch=exc_batch)
  File "./collector_gapi/batch_handler.py", line 253, in run_batch
    item_list, service_func, batch_parse_response, batch_request_dict)
  File "./collector_gapi/batch_handler.py", line 94, in calculate_ideal_length
    item_list, 1, service_func, callback, batch_request_dict)
  File "./collector_gapi/batch_handler.py", line 72, in compile_batch
    return batch, get_payload_size(batch)
  File "./collector_gapi/batch_handler.py", line 42, in get_payload_size
    body = batch._serialize_request(request)
  File "/home/seodev/project/venv/lib/python3.4/site-packages/googleapiclient/http.py", line 1101, in _serialize_request
    request.http.request.credentials.apply(headers)
  File "/home/seodev/project/venv/lib/python3.4/site-packages/oauth2client/client.py", line 643, in apply
    headers['Authorization'] = 'Bearer ' + self.access_token
TypeError: Can't convert 'NoneType' object to str implicitly

This exception doesn’t occur with google-api-python-client==1.4.1, so 1.4.2 is definitely the culprit.

Could you please look into this and fix it?

Thx, László

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kkuriancommented, Jun 5, 2016

Seeing this issue intermittently in 1.5.1.

The following workaround, which builds off of the previously reported workaround, resolves the issue for me so far:

try:
    batch.execute()
except TypeError:
    credentials.refresh(http)
    batch.execute()
1reaction
dobesvcommented, Nov 24, 2015

As a workaround, I can force refresh the credentials prior to use:

import json
creds = json.load(open('dev/etc/google-service-accounts/enterprise.json'))
from httplib2 import Http
from oauth2client.client import SignedJwtAssertionCredentials

credentials = SignedJwtAssertionCredentials(
            creds['client_email'],
            creds['private_key'],
            'https://www.googleapis.com/auth/androidenterprise'
        )
http = credentials.authorize(Http())
credentials.refresh(http)  # batch() doesn't refresh the access_token on time
from googleapiclient.discovery import build
service = build('androidenterprise', 'v1', http=http)
batch = service.new_batch_http_request()
batch.add(service.installs().update(userId='1', enterpriseId='2', deviceId='3', installId='4', body={}))
batch.add(service.installs().update(userId='2', enterpriseId='2', deviceId='4', installId='4', body={}))
batch.add(service.installs().update(userId='3', enterpriseId='2', deviceId='5', installId='4', body={}))
batch.execute()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Service account API error when getting the access token
I created new project in the google console (https://console.cloud.google.com/) I enabled People API. I created needed Credentials - web client ...
Read more >
Using OAuth 2.0 for Server to Server Applications | Authorization
A service account's credentials include a generated email address that is unique and at least one public/private key pair. If domain-wide delegation is...
Read more >
Create short-lived credentials for a service account
Service accounts can use short-lived credentials to authenticate calls to Google Cloud APIs, other Google APIs, and non-Google APIs. Short-lived credentials ...
Read more >
google.oauth2.service_account module
If this is None, the token is assumed to never expire. Open ID Connect ID Token-based service account credentials. These credentials are largely...
Read more >
Creating a personal access token - GitHub Docs
Under Permissions, select which permissions to grant the token. Depending on which resource owner and which repository access you specified, there are ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found