前提・実現したいこと
現在、ReactとGoogle AnalyticsのAPIを使用してWEBアプリの作成をしています。
consoleの方で、OAuth 2.0 クライアント IDとAPI キーは作成しています。
また、analyticsのAPIの使用許可も行っております。
実現したいことはGoogle AnalyticsのAPIのレスポンスを取得したいです。
現在、エラーは403が返ってきています。
https://developers.google.com/analytics/devguides/reporting/core/v4/errors?hl=ja
発生している問題・エラーメッセージ
Uncaught (in promise) { "error": { "code": 403, "message": "Request had insufficient authentication scopes.", "status": "PERMISSION_DENIED" } }
該当のソースコード
以下のソースコードはReactとTypeScriptを使用しているため、tsxでの実装となっております。
typescript
const start = async () => { await gapi.client.init({ apiKey: '<apiKey>', discoveryDocs: ['https://analyticsreporting.googleapis.com/$discovery/rest?version=v4'], clientId: '<clientId>', scope: 'profile', }); gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus); updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get()); }; const clickHandler = useCallback(async () => { const client = await gapi.client.request({ path: 'https://analyticsreporting.googleapis.com/v4/reports:batchGet', method: 'POST', body: { reportRequests: [ { viewId: '191284033', dateRanges: [ { startDate: '2020-03-01', endDate: '2020-03-10', }, ], metrics: [ { expression: 'ga:sessions', }, ], }, ], }, }); // eslint-disable-next-line no-console console.log({ client }); }, []); function updateSigninStatus(isSignedIn: boolean) { setLogin(isSignedIn); }
試したこと
https://github.com/google/google-api-javascript-client/blob/master/docs/start.md
https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/gapi.client.analyticsreporting
調べたところ、gapi.client.initやgapi.auth.authorizeなどで認証をするの方法が出てきたので、試してみたいのですが、現在分からず困っています。
回答のほど、何卒よろしくお願いします。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
まだ回答がついていません
会員登録して回答してみよう