Google-api-python-client: cannot import name 'ServiceAccountCredentials' from 'oauth2client.service_account'

Created on 2 Feb 2019  路  10Comments  路  Source: googleapis/google-api-python-client

I want to use the library googleanalytics in my python file, but the code would encounter the following error.
cannot import name 'ServiceAccountCredentials' from 'oauth2client.service_account'
My command is :
from oauth2client.service_account import ServiceAccountCredentials
I have been searching much in different cites and installed all the related library, but it didn't work.
My python version is 3.7.2

docs

Most helpful comment

Hello guys, found out something in one tutorial. Instead of using this:

import httplib2
import apiclient.discovery
from oauth2client.service_account import ServiceAccountCredentials

credentials = ServiceAccountCredentials.from_json_keyfile_name(json_key_filename, SCOPES)
httpAuth = credentials.authorize(httplib2.Http())
service = apiclient.discovery.build('sheets', 'v4', http = httpAuth)

You should use this:

from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build

credentials = Credentials.from_service_account_file(json_key_filename, scopes=SCOPES)
service = build('sheets', 'v4', credentials=credentials)

I'm new to GitHub, signed up just for this comment)

All 10 comments

Did you solve this? I'm experiencing the same issue.

python 2.7, same issue.
Name: oauth2client Version: 4.1.3

Hello,

oauth2client was deprecated last year. We recommend that you use google-auth.

I'm not able to reproduce this with a clean virtualenv in 2.7 or 3.7.

Could you verify that oauth2client is installed? Are you able to import oauth2client?

Hello guys, found out something in one tutorial. Instead of using this:

import httplib2
import apiclient.discovery
from oauth2client.service_account import ServiceAccountCredentials

credentials = ServiceAccountCredentials.from_json_keyfile_name(json_key_filename, SCOPES)
httpAuth = credentials.authorize(httplib2.Http())
service = apiclient.discovery.build('sheets', 'v4', http = httpAuth)

You should use this:

from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build

credentials = Credentials.from_service_account_file(json_key_filename, scopes=SCOPES)
service = build('sheets', 'v4', credentials=credentials)

I'm new to GitHub, signed up just for this comment)

Seems to work okay for python 3.4

You should reference the second code snippet @Lamroy95 posted and use google-auth rather than oauth2client. Closing as the original issue has been resolved. Please do open a new issue if you have more questions.

Hello guys, find out something in one tutorial. Instead of using this:

import httplib2
import apiclient.discovery
from oauth2client.service_account import ServiceAccountCredentials

credentials = ServiceAccountCredentials.from_json_keyfile_name(json_key_filename, SCOPES)
httpAuth = credentials.authorize(httplib2.Http())
service = apiclient.discovery.build('sheets', 'v4', http = httpAuth)

You should use this:

from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build

credentials = Credentials.from_service_account_file(json_key_filename, scopes=SCOPES)
service = build('sheets', 'v4', credentials=credentials)

I'm new to GitHub, signed up just for this comment)

Thank you very much Lamroy95

Hello guys, find out something in one tutorial. Instead of using this:

import httplib2
import apiclient.discovery
from oauth2client.service_account import ServiceAccountCredentials

credentials = ServiceAccountCredentials.from_json_keyfile_name(json_key_filename, SCOPES)
httpAuth = credentials.authorize(httplib2.Http())
service = apiclient.discovery.build('sheets', 'v4', http = httpAuth)

You should use this:

from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build

credentials = Credentials.from_service_account_file(json_key_filename, scopes=SCOPES)
service = build('sheets', 'v4', credentials=credentials)

I'm new to GitHub, signed up just for this comment)

Hey,do you have a similar code for Python?

Hey,do you have a similar code for Python?

It's a python 3 code :|

Hey,do you have a similar code for Python

Hey,do you have a similar code for Python?

It's a python 3 code :|

I'd used this one initially,please can u temme the edits for this?(cuz it doesnt have the first 2 lines(import) ones-)
"""Hello Analytics Reporting API V4."""

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
KEY_FILE_LOCATION = ''
VIEW_ID = ''

def initialize_analyticsreporting():
"""Initializes an Analytics Reporting API V4 service object.

Returns:
An authorized Analytics Reporting API V4 service object.
"""
credentials = ServiceAccountCredentials.from_json_keyfile_name(
KEY_FILE_LOCATION, SCOPES)

# Build the service object.
analytics = build('analyticsreporting', 'v4', credentials=credentials)

return analytics

def get_report(analytics):
"""Queries the Analytics Reporting API V4.

Args:
analytics: An authorized Analytics Reporting API V4 service object.
Returns:
The Analytics Reporting API V4 response.
"""
return analytics.reports().batchGet(
body={
'reportRequests': [
{
'viewId': VIEW_ID,
'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
'metrics': [{'expression': 'ga:sessions'}],
'dimensions': [{'name': 'ga:country'}]
}]
}
).execute()

def print_response(response):
"""Parses and prints the Analytics Reporting API V4 response.

Args:
response: An Analytics Reporting API V4 response.
"""
for report in response.get('reports', []):
columnHeader = report.get('columnHeader', {})
dimensionHeaders = columnHeader.get('dimensions', [])
metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])

for row in report.get('data', {}).get('rows', []):
  dimensions = row.get('dimensions', [])
  dateRangeValues = row.get('metrics', [])

  for header, dimension in zip(dimensionHeaders, dimensions):
    print header + ': ' + dimension

  for i, values in enumerate(dateRangeValues):
    print 'Date range: ' + str(i)
    for metricHeader, value in zip(metricHeaders, values.get('values')):
      print metricHeader.get('name') + ': ' + value

def main():
analytics = initialize_analyticsreporting()
response = get_report(analytics)
print_response(response)

if __name__ == '__main__':
main()

Was this page helpful?
0 / 5 - 0 ratings

Related issues

knusperkrone picture knusperkrone  路  7Comments

lshchur picture lshchur  路  3Comments

jacobtabak picture jacobtabak  路  4Comments

pacohh picture pacohh  路  7Comments

OmerJog picture OmerJog  路  4Comments