前提・実現したいこと
GoogleAnalyticsのAPIサンプルをpythonで実行しました。
発生している問題・エラーメッセージ
IsADirectoryError: [Errno 21] Is a directory:```
参考ソース
python
1"""A simple example of how to access the Google Analytics API.""" 2 3from apiclient.discovery import build 4from oauth2client.service_account import ServiceAccountCredentials 5 6def get_service(api_name, api_version, scopes, key_file_location): 7 """Get a service that communicates to a Google API. 8 9 Args: 10 api_name: The name of the api to connect to. 11 api_version: The api version to connect to. 12 scopes: A list auth scopes to authorize for the application. 13 key_file_location: The path to a valid service account JSON key file. 14 15 Returns: 16 A service that is connected to the specified API. 17 """ 18 19 credentials = ServiceAccountCredentials.from_json_keyfile_name( 20 key_file_location, scopes=scopes) 21 22 # Build the service object. 23 service = build(api_name, api_version, credentials=credentials) 24 25 return service 26 27def get_first_profile_id(service): 28 # Use the Analytics service object to get the first profile id. 29 30 # Get a list of all Google Analytics accounts for this user 31 accounts = service.management().accounts().list().execute() 32 33 if accounts.get('items'): 34 # Get the first Google Analytics account. 35 account = accounts.get('items')[0].get('id') 36 37 # Get a list of all the properties for the first account. 38 properties = service.management().webproperties().list( 39 accountId=account).execute() 40 41 if properties.get('items'): 42 # Get the first property id. 43 property = properties.get('items')[0].get('id') 44 45 # Get a list of all views (profiles) for the first property. 46 profiles = service.management().profiles().list( 47 accountId=account, 48 webPropertyId=property).execute() 49 50 if profiles.get('items'): 51 # return the first view (profile) id. 52 return profiles.get('items')[0].get('id') 53 54 return None 55 56def get_results(service, profile_id): 57 # Use the Analytics Service Object to query the Core Reporting API 58 # for the number of sessions within the past seven days. 59 return service.data().ga().get( 60 ids='ga:' + profile_id, 61 start_date='7daysAgo', 62 end_date='today', 63 metrics='ga:sessions').execute() 64 65def print_results(results): 66 # Print data nicely for the user. 67 if results: 68 print 'View (Profile):', results.get('profileInfo').get('profileName') 69 print 'Total Sessions:', results.get('rows')[0][0] 70 71 else: 72 print 'No results found' 73 74def main(): 75 # Define the auth scopes to request. 76 scope = 'https://www.googleapis.com/auth/analytics.readonly' 77 key_file_location = '<REPLACE_WITH_JSON_FILE>' 78 79 # Authenticate and construct service. 80 service = get_service( 81 api_name='analytics', 82 api_version='v3', 83 scopes=[scope], 84 key_file_location=key_file_location) 85 86 profile_id = get_first_profile_id(service) 87 print_results(get_results(service, profile_id)) 88 89if __name__ == '__main__': 90 main()``` 91
試したこと
上記ソースの実行
あなたの回答
tips
プレビュー