2

I have set up a service account for my google calendar api project. The idea is that my web site can connect to my google calendar and display events, etc. As it was put by google

Service account clients are created when domain-wide delegation is 
enabled on a service account.

Which indeed happened and so I have a connecting OAuth 2.0 client ID. So far so good. I generated a json file that I used to connect and try to authenticate using this code

from cal.models import Cal as Caldb
import httplib2

from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client import tools

from calendar import monthrange
from calendar_gui import MyCalendar
from datetime import datetime 
from datetime import date
from django.utils.safestring import mark_safe

from oauth2client.service_account import ServiceAccountCredentials
import os
from django.conf import settings

def authenticate():
    scope= ['https://www.googleapis.com/auth/calendar']
    credentials = ServiceAccountCredentials.from_json_keyfile_name(os.path.join(settings.PROJECT_ROOT, '../', 'myjson.json'), scopes=scope)
    delegated_credentials = credentials.create_delegated('email@email.com')

    http = httplib2.Http()
    http = delegated_credentials.authorize(http)
    service = build(serviceName='calendar', version='v3', http=http, credentials=credentials)

    return service

But alas

service = build(serviceName='calendar', version='v3', http=http, credentials=credentials)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 137, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/googleapiclient/discovery.py", line 214, in build
cache)
File "/usr/local/lib/python2.7/dist-packages/googleapiclient/discovery.py", line 261, in _retrieve_discovery_doc
resp, content = http.request(actual_url)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/transport.py", line 153, in new_request
credentials._refresh(orig_request_method)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 765, in _refresh
self._do_refresh_request(http_request)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 834, in _do_refresh_request
  raise HttpAccessTokenRefreshError(error_msg, status=resp.status)
HttpAccessTokenRefreshError: unauthorized_client: Client is unauthorized to retrieve access tokens using this method.    

I have enabled the google calendar api. In fact, I feel like I've followed all of the steps. In fact, I've done this once before with complete success, but I'm perplexed now. I read that maybe I need to wait 24-48 hours before this all takes effect. Could it be as simple as that?

For right now, I'm just testing this locally. Nothing pushed to the server yet.

Matt Cremeens
  • 4,951
  • 7
  • 38
  • 67

1 Answers1

0

Check this link out: Google API Python unauthorized_client: Unauthorized client or scope in request

You need to use the client ID from your "Developer Console" as the Client Name in the "Manage API Client Access" when setting your API scopes.

Danee
  • 322
  • 1
  • 4
  • I saw that, but I'm just trying to connect with the `json` file that was automatically generated for me, along with the client key. – Matt Cremeens Jul 10 '17 at 17:28