質問編集履歴

1

コードの追加

2019/08/03 04:54

投稿

GenkiSugiyama
GenkiSugiyama

スコア86

test CHANGED
File without changes
test CHANGED
@@ -19,3 +19,333 @@
19
19
  上記二点についてご教授いただけますと大変助かります。
20
20
 
21
21
  よろしくお願いいたします。
22
+
23
+
24
+
25
+ ※コード追加のご指摘をいただいたので記載いたします。
26
+
27
+ ```auth_util.js
28
+
29
+ // Copyright 2012 Google Inc. All Rights Reserved.
30
+
31
+
32
+
33
+ /* Licensed under the Apache License, Version 2.0 (the "License");
34
+
35
+ * you may not use this file except in compliance with the License.
36
+
37
+ * You may obtain a copy of the License at
38
+
39
+ *
40
+
41
+ * http://www.apache.org/licenses/LICENSE-2.0
42
+
43
+ *
44
+
45
+ * Unless required by applicable law or agreed to in writing, software
46
+
47
+ * distributed under the License is distributed on an "AS IS" BASIS,
48
+
49
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
50
+
51
+ * See the License for the specific language governing permissions and
52
+
53
+ * limitations under the License.
54
+
55
+ */
56
+
57
+
58
+
59
+ /**
60
+
61
+ * @fileoverview Utility for handling authorization and updating the UI
62
+
63
+ * accordingy.
64
+
65
+ * @author api.nickm@gmail.com (Nick Mihailovski)
66
+
67
+ */
68
+
69
+
70
+
71
+ /**
72
+
73
+ * Authorization information. This should be obtained through the Google APIs
74
+
75
+ * developers console. https://code.google.com/apis/console/
76
+
77
+ * Also there is more information about how to get these in the authorization
78
+
79
+ * section in the Google JavaScript Client Library.
80
+
81
+ * https://code.google.com/p/google-api-javascript-client/wiki/Authentication
82
+
83
+ */
84
+
85
+ var clientId = 'xxxxxxxxxxxxxxx';
86
+
87
+ var apiKey = 'xxxxxxxxxxxxxxx';
88
+
89
+ var scopes = 'https://www.googleapis.com/auth/analytics.readonly';
90
+
91
+
92
+
93
+ /**
94
+
95
+ * Callback executed once the Google APIs Javascript client library has loaded.
96
+
97
+ * The function name is specified in the onload query parameter of URL to load
98
+
99
+ * this library. After 1 millisecond, checkAuth is called.
100
+
101
+ */
102
+
103
+ function handleClientLoad() {
104
+
105
+ gapi.client.setApiKey(apiKey);
106
+
107
+ window.setTimeout(checkAuth, 1);
108
+
109
+ }
110
+
111
+
112
+
113
+ /**
114
+
115
+ * Uses the OAuth2.0 clientId to query the Google Accounts service
116
+
117
+ * to see if the user has authorized. Once complete, handleAuthResults is
118
+
119
+ * called.
120
+
121
+ */
122
+
123
+ function checkAuth() {
124
+
125
+ gapi.auth.authorize({
126
+
127
+ client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
128
+
129
+ }
130
+
131
+
132
+
133
+ /**
134
+
135
+ * Handler that is called once the script has checked to see if the user has
136
+
137
+ * authorized access to their Google Analytics data. If the user has authorized
138
+
139
+ * access, the analytics api library is loaded and the handleAuthorized
140
+
141
+ * function is executed. If the user has not authorized access to their data,
142
+
143
+ * the handleUnauthorized function is executed.
144
+
145
+ * @param {Object} authResult The result object returned form the authorization
146
+
147
+ * service that determine whether the user has currently authorized access
148
+
149
+ * to their data. If it exists, the user has authorized access.
150
+
151
+ */
152
+
153
+ function handleAuthResult(authResult) {
154
+
155
+ if (authResult && !authResult.error) {
156
+
157
+ gapi.client.load('analytics', 'v3', handleAuthorized);
158
+
159
+ } else {
160
+
161
+ handleUnauthorized();
162
+
163
+ }
164
+
165
+ }
166
+
167
+
168
+
169
+ /**
170
+
171
+ * Updates the UI once the user has authorized this script to access their
172
+
173
+ * data. This changes the visibiilty on some buttons and adds the
174
+
175
+ * makeApiCall click handler to the run-demo-button.
176
+
177
+ */
178
+
179
+ function handleAuthorized() {
180
+
181
+ var authorizeButton = document.getElementById('authorize-button');
182
+
183
+ var runDemoButton = document.getElementById('run-demo-button');
184
+
185
+
186
+
187
+ authorizeButton.style.visibility = 'hidden';
188
+
189
+ runDemoButton.style.visibility = '';
190
+
191
+ runDemoButton.onclick = makeApiCall;
192
+
193
+ outputToPage('Click the Run Demo button to begin.');
194
+
195
+ }
196
+
197
+
198
+
199
+ /**
200
+
201
+ * Updates the UI if a user has not yet authorized this script to access
202
+
203
+ * their Google Analytics data. This function changes the visibility of
204
+
205
+ * some elements on the screen. It also adds the handleAuthClick
206
+
207
+ * click handler to the authorize-button.
208
+
209
+ */
210
+
211
+ function handleUnauthorized() {
212
+
213
+ var authorizeButton = document.getElementById('authorize-button');
214
+
215
+ var runDemoButton = document.getElementById('run-demo-button');
216
+
217
+
218
+
219
+ runDemoButton.style.visibility = 'hidden';
220
+
221
+ authorizeButton.style.visibility = '';
222
+
223
+ authorizeButton.onclick = handleAuthClick;
224
+
225
+ outputToPage('Please authorize this script to access Google Analytics.');
226
+
227
+ }
228
+
229
+
230
+
231
+ /**
232
+
233
+ * Handler for clicks on the authorization button. This uses the OAuth2.0
234
+
235
+ * clientId to query the Google Accounts service to see if the user has
236
+
237
+ * authorized. Once complete, handleAuthResults is called.
238
+
239
+ * @param {Object} event The onclick event.
240
+
241
+ */
242
+
243
+ function handleAuthClick(event) {
244
+
245
+ gapi.auth.authorize({
246
+
247
+ client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
248
+
249
+ return false;
250
+
251
+ }
252
+
253
+
254
+
255
+ ```
256
+
257
+
258
+
259
+ ```hello_analytics_api_v3.html
260
+
261
+ <!DOCTYPE>
262
+
263
+ <html>
264
+
265
+ <head>
266
+
267
+ <title>Hello Google Analytics API</title>
268
+
269
+ <link rel="stylesheet" type="text/css"
270
+
271
+ href="//fonts.googleapis.com/css?family=Cantarell|Inconsolata">
272
+
273
+ <link rel="stylesheet" type="text/css"
274
+
275
+ href="pretty.css">
276
+
277
+ </head>
278
+
279
+ <body>
280
+
281
+ <h1>Hello Google Analytics API</h1>
282
+
283
+ <p>This page demonstrates how to query the Google Analytics API version 3 using the
284
+
285
+ <a href="https://code.google.com/p/google-api-javascript-client/">
286
+
287
+ Google API Javascript Client Library</a>.</p>
288
+
289
+
290
+
291
+ <p>The demo traverses the
292
+
293
+ <a href="http://code.google.com/apis/analytics/docs/mgmt/v3/mgmtGettingStarted.html">
294
+
295
+ Management API</a> to retrieve the first profile ID for the
296
+
297
+ authorized user. This profile ID is then used with the
298
+
299
+ <a href="http://code.google.com/apis/analytics/docs/gdata/v3/gdataGettingStarted.html">
300
+
301
+ Core Reporting API</a> to retrieve the top 25 organic search terms by visits.</p>
302
+
303
+
304
+
305
+ <p>To learn how this works, read the detailed comments in the
306
+
307
+ <a href="hello_analytics_api_v3.js">JavaScript source code</a>.</p>
308
+
309
+
310
+
311
+ <p>To run the demo:</p>
312
+
313
+ <ol>
314
+
315
+ <li>Click the authorize button to grant access to your Google Analytics data.</li>
316
+
317
+ <li>Click the run demo button to start the demo.</li>
318
+
319
+ </ol>
320
+
321
+
322
+
323
+ <hr>
324
+
325
+ <button id="authorize-button" style="visibility: hidden">Authorize</button>
326
+
327
+ <button id="run-demo-button" >Run Demo</button>
328
+
329
+ <br>
330
+
331
+ <hr>
332
+
333
+ <div id="output">Loading, one sec....</div>
334
+
335
+
336
+
337
+ <script src="auth_util.js"></script>
338
+
339
+ <script src="hello_analytics_api_v3.js"></script>
340
+
341
+ <script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
342
+
343
+
344
+
345
+ </body>
346
+
347
+ </html>
348
+
349
+
350
+
351
+ ```