Shankar
Shankar

Reputation: 2825

Error creating Blogger API end point using Google API Client defaut credentails

I am trying to create a Blogger API service object in the Google Cloud function. I am referrin to the example in https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application (section "Obtaining credentials on Compute Engine, Kubernetes Engine, App Engine flexible environment, and Cloud Functions")

The example given is for Google Starage. I am trying to convert it to get the Google API client for the Blogger API. I could not find the documentation on how to use this default credential to create Blogger API service object. I tried to reuse the code from

I tried the following code, but I get error. I am aware this is wrong, but I am stuck with no documentation on how to use this credential.

from google.auth import compute_engine
import googleapiclient.discovery

credentials = compute_engine.Credentials()
print(type(credentials))

blogger = googleapiclient.discovery.build('blogger', 'v3', credentials=credentials)
print(blogger)

I get the error below:

E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam Traceback (most recent call last): TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam   File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 33, in <module> TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam     from oauth2client.contrib.locked_file import LockedFile TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam ModuleNotFoundError: No module named 'oauth2client' TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam  TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam During handling of the above exception, another exception occurred: TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam  TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam Traceback (most recent call last): TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam   File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 37, in <module> TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam     from oauth2client.locked_file import LockedFile TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam ModuleNotFoundError: No module named 'oauth2client' TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam  TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam During handling of the above exception, another exception occurred: TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam  TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam Traceback (most recent call last): TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam   File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/__init__.py", line 41, in autodetect TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam     from . import file_cache TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam   File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 41, in <module> TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam     'file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth') TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam ImportError: file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth TestHTTPFunction h8log1bk5kam 
I 2020-01-01T20:16:08.656Z TestHTTPFunction h8log1bk5kam URL being requested: GET https://www.googleapis.com/discovery/v1/apis/blogger/v3/rest TestHTTPFunction h8log1bk5kam 
I 2020-01-01T20:16:08.723Z TestHTTPFunction h8log1bk5kam <googleapiclient.discovery.Resource object at 0x7ec9066c9710> TestHTTPFunction h8log1bk5kam 
I 2020-01-01T20:16:08.724Z TestHTTPFunction h8log1bk5kam URL being requested: GET https://www.googleapis.com/blogger/v3/blogs/2709229652340798222?maxPosts=2&alt=json TestHTTPFunction h8log1bk5kam 
E 2020-01-01T20:16:08.752Z TestHTTPFunction h8log1bk5kam Encountered 403 Forbidden with reason "insufficientPermissions" TestHTTPFunction h8log1bk5kam 

Upvotes: 0

Views: 201

Answers (1)

Soni Sol
Soni Sol

Reputation: 2612

The issue as I see so far is that you are using the Client Library for Google Compute Engine( to connect to GCP Virtual Machines) but you want to connect to Blogger.

Here is an example of how to use the right client libraries to connect to Blogger.

The way to manage the credentials is having a credential.json file with the following stucture:


{
  "web": {
    "client_id": "[[INSERT CLIENT ID HERE]]",
    "client_secret": "[[INSERT CLIENT SECRET HERE]]",
    "redirect_uris": [],
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://accounts.google.com/o/oauth2/token"
  }
}

On your Python code have this:

import sys

from oauth2client import client
from googleapiclient import sample_tools


def main(argv):
  # Authenticate and construct service.
  service, flags = sample_tools.init(
      argv, 'blogger', 'v3', __doc__, __file__,
      scope='https://www.googleapis.com/auth/blogger')
  try:
      users = service.users()
      thisuser = users.get(userId='self').execute()

Upvotes: 1

Related Questions