質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Google Analytics

Google AnalyticsはGoogleが開発した無料のウェブ分析のソリューションです。複数のクライアント側のAPIとデータをエクスポートし管理するREST APIも格納されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

0回答

3632閲覧

IsADirectoryError: [Errno 21] Is a directory:の解決法

peipei1993

総合スコア12

Google Analytics

Google AnalyticsはGoogleが開発した無料のウェブ分析のソリューションです。複数のクライアント側のAPIとデータをエクスポートし管理するREST APIも格納されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2019/01/11 01:53

編集2019/01/11 02:16

前提・実現したいこと

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

試したこと

上記ソースの実行

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2019/01/11 01:55

このままではコードが読めないため、質門を編集して、<code>ボタン、出てきた’’’の枠の中にコードを貼り付けてください
peipei1993

2019/01/11 02:04

すみません! 修正しました!!
hayataka2049

2019/01/11 03:27

インデントが潰れてしまっているので修正してください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問