google-api-client 0.52.0 → 0.53.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (264) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +0 -14
  3. data/CHANGELOG.md +6 -0
  4. data/OVERVIEW.md +36 -27
  5. data/generated/google/apis/accesscontextmanager_v1.rb +1 -1
  6. data/generated/google/apis/accesscontextmanager_v1/classes.rb +326 -0
  7. data/generated/google/apis/accesscontextmanager_v1/representations.rb +139 -0
  8. data/generated/google/apis/admin_directory_v1.rb +1 -1
  9. data/generated/google/apis/admin_directory_v1/service.rb +2 -2
  10. data/generated/google/apis/analyticsadmin_v1alpha.rb +2 -2
  11. data/generated/google/apis/analyticsadmin_v1alpha/classes.rb +8 -0
  12. data/generated/google/apis/analyticsadmin_v1alpha/representations.rb +1 -0
  13. data/generated/google/apis/analyticsadmin_v1alpha/service.rb +12 -1
  14. data/{lib/google/apis/core/logging.rb → generated/google/apis/apigateway_v1.rb} +14 -10
  15. data/generated/google/apis/apigateway_v1/classes.rb +1116 -0
  16. data/generated/google/apis/apigateway_v1/representations.rb +442 -0
  17. data/generated/google/apis/apigateway_v1/service.rb +1172 -0
  18. data/generated/google/apis/apigateway_v1beta.rb +1 -1
  19. data/generated/google/apis/apigateway_v1beta/classes.rb +19 -8
  20. data/generated/google/apis/apigateway_v1beta/representations.rb +1 -0
  21. data/generated/google/apis/apigee_v1.rb +1 -1
  22. data/generated/google/apis/apigee_v1/classes.rb +24 -22
  23. data/generated/google/apis/apigee_v1/service.rb +50 -48
  24. data/generated/google/apis/artifactregistry_v1.rb +38 -0
  25. data/generated/google/apis/artifactregistry_v1/classes.rb +242 -0
  26. data/generated/google/apis/artifactregistry_v1/representations.rb +116 -0
  27. data/generated/google/apis/artifactregistry_v1/service.rb +210 -0
  28. data/generated/google/apis/artifactregistry_v1beta1.rb +1 -1
  29. data/generated/google/apis/artifactregistry_v1beta1/classes.rb +0 -120
  30. data/generated/google/apis/artifactregistry_v1beta1/representations.rb +0 -62
  31. data/generated/google/apis/artifactregistry_v1beta2.rb +1 -1
  32. data/generated/google/apis/artifactregistry_v1beta2/classes.rb +0 -120
  33. data/generated/google/apis/artifactregistry_v1beta2/representations.rb +0 -62
  34. data/generated/google/apis/chat_v1.rb +1 -1
  35. data/generated/google/apis/chat_v1/service.rb +132 -0
  36. data/generated/google/apis/cloudasset_v1.rb +1 -1
  37. data/generated/google/apis/cloudasset_v1/classes.rb +326 -0
  38. data/generated/google/apis/cloudasset_v1/representations.rb +139 -0
  39. data/generated/google/apis/cloudasset_v1beta1.rb +1 -1
  40. data/generated/google/apis/cloudasset_v1beta1/classes.rb +326 -0
  41. data/generated/google/apis/cloudasset_v1beta1/representations.rb +139 -0
  42. data/generated/google/apis/cloudasset_v1p5beta1.rb +1 -1
  43. data/generated/google/apis/cloudasset_v1p5beta1/classes.rb +326 -0
  44. data/generated/google/apis/cloudasset_v1p5beta1/representations.rb +139 -0
  45. data/generated/google/apis/cloudbuild_v1.rb +1 -1
  46. data/generated/google/apis/cloudbuild_v1/classes.rb +3 -3
  47. data/generated/google/apis/cloudbuild_v1alpha1.rb +1 -1
  48. data/generated/google/apis/cloudbuild_v1alpha1/classes.rb +3 -3
  49. data/generated/google/apis/cloudbuild_v1alpha2.rb +1 -1
  50. data/generated/google/apis/cloudbuild_v1alpha2/classes.rb +3 -3
  51. data/generated/google/apis/cloudidentity_v1beta1.rb +1 -1
  52. data/generated/google/apis/cloudidentity_v1beta1/classes.rb +1 -1
  53. data/generated/google/apis/cloudresourcemanager_v1.rb +1 -1
  54. data/generated/google/apis/cloudresourcemanager_v1/classes.rb +0 -26
  55. data/generated/google/apis/cloudresourcemanager_v1/representations.rb +0 -24
  56. data/generated/google/apis/cloudresourcemanager_v1beta1.rb +1 -1
  57. data/generated/google/apis/cloudresourcemanager_v1beta1/classes.rb +0 -26
  58. data/generated/google/apis/cloudresourcemanager_v1beta1/representations.rb +0 -24
  59. data/generated/google/apis/cloudresourcemanager_v2.rb +1 -1
  60. data/generated/google/apis/cloudresourcemanager_v2/classes.rb +0 -26
  61. data/generated/google/apis/cloudresourcemanager_v2/representations.rb +0 -24
  62. data/generated/google/apis/cloudresourcemanager_v2beta1.rb +1 -1
  63. data/generated/google/apis/cloudresourcemanager_v2beta1/classes.rb +0 -26
  64. data/generated/google/apis/cloudresourcemanager_v2beta1/representations.rb +0 -24
  65. data/generated/google/apis/container_v1.rb +1 -1
  66. data/generated/google/apis/container_v1/classes.rb +7 -7
  67. data/generated/google/apis/container_v1beta1.rb +1 -1
  68. data/generated/google/apis/container_v1beta1/classes.rb +7 -7
  69. data/generated/google/apis/content_v2_1.rb +1 -1
  70. data/generated/google/apis/content_v2_1/classes.rb +434 -0
  71. data/generated/google/apis/content_v2_1/representations.rb +187 -0
  72. data/generated/google/apis/content_v2_1/service.rb +116 -0
  73. data/generated/google/apis/datacatalog_v1beta1.rb +1 -1
  74. data/generated/google/apis/datacatalog_v1beta1/classes.rb +6 -0
  75. data/generated/google/apis/datacatalog_v1beta1/representations.rb +1 -0
  76. data/generated/google/apis/dataflow_v1b3.rb +1 -1
  77. data/generated/google/apis/datalabeling_v1beta1.rb +1 -1
  78. data/generated/google/apis/dataproc_v1.rb +1 -1
  79. data/generated/google/apis/dataproc_v1/classes.rb +29 -1
  80. data/generated/google/apis/dataproc_v1/representations.rb +14 -0
  81. data/generated/google/apis/dataproc_v1/service.rb +42 -0
  82. data/generated/google/apis/dataproc_v1beta2.rb +1 -1
  83. data/generated/google/apis/dataproc_v1beta2/classes.rb +29 -1
  84. data/generated/google/apis/dataproc_v1beta2/representations.rb +14 -0
  85. data/generated/google/apis/dataproc_v1beta2/service.rb +42 -0
  86. data/generated/google/apis/dialogflow_v2.rb +1 -1
  87. data/generated/google/apis/dialogflow_v2/classes.rb +4041 -678
  88. data/generated/google/apis/dialogflow_v2/representations.rb +1851 -298
  89. data/generated/google/apis/dialogflow_v2beta1.rb +1 -1
  90. data/generated/google/apis/dialogflow_v2beta1/classes.rb +4047 -684
  91. data/generated/google/apis/dialogflow_v2beta1/representations.rb +1862 -309
  92. data/generated/google/apis/dialogflow_v3.rb +1 -1
  93. data/generated/google/apis/dialogflow_v3/classes.rb +3120 -311
  94. data/generated/google/apis/dialogflow_v3/representations.rb +2002 -617
  95. data/generated/google/apis/dialogflow_v3/service.rb +408 -0
  96. data/generated/google/apis/dialogflow_v3beta1.rb +1 -1
  97. data/generated/google/apis/dialogflow_v3beta1/classes.rb +5145 -2336
  98. data/generated/google/apis/dialogflow_v3beta1/representations.rb +1729 -344
  99. data/generated/google/apis/dialogflow_v3beta1/service.rb +408 -0
  100. data/generated/google/apis/dlp_v2.rb +1 -1
  101. data/generated/google/apis/dlp_v2/service.rb +3 -9
  102. data/generated/google/apis/documentai_v1beta2.rb +1 -1
  103. data/generated/google/apis/documentai_v1beta2/classes.rb +5 -92
  104. data/generated/google/apis/documentai_v1beta2/representations.rb +2 -15
  105. data/generated/google/apis/documentai_v1beta3.rb +1 -1
  106. data/generated/google/apis/documentai_v1beta3/classes.rb +5 -92
  107. data/generated/google/apis/documentai_v1beta3/representations.rb +2 -15
  108. data/generated/google/apis/gameservices_v1.rb +1 -1
  109. data/generated/google/apis/gameservices_v1/service.rb +14 -14
  110. data/generated/google/apis/gameservices_v1beta.rb +1 -1
  111. data/generated/google/apis/gameservices_v1beta/service.rb +14 -14
  112. data/generated/google/apis/localservices_v1.rb +4 -1
  113. data/generated/google/apis/manufacturers_v1.rb +1 -1
  114. data/generated/google/apis/manufacturers_v1/classes.rb +2 -2
  115. data/generated/google/apis/monitoring_v3.rb +1 -1
  116. data/generated/google/apis/monitoring_v3/classes.rb +2 -2
  117. data/generated/google/apis/notebooks_v1.rb +1 -1
  118. data/generated/google/apis/notebooks_v1/classes.rb +13 -0
  119. data/generated/google/apis/notebooks_v1/representations.rb +12 -0
  120. data/generated/google/apis/notebooks_v1/service.rb +34 -0
  121. data/generated/google/apis/prod_tt_sasportal_v1alpha1.rb +1 -1
  122. data/generated/google/apis/prod_tt_sasportal_v1alpha1/classes.rb +0 -37
  123. data/generated/google/apis/prod_tt_sasportal_v1alpha1/representations.rb +0 -18
  124. data/generated/google/apis/recommendationengine_v1beta1.rb +1 -1
  125. data/generated/google/apis/recommendationengine_v1beta1/service.rb +2 -1
  126. data/generated/google/apis/securitycenter_v1.rb +1 -1
  127. data/generated/google/apis/securitycenter_v1/classes.rb +18 -10
  128. data/generated/google/apis/securitycenter_v1/representations.rb +1 -0
  129. data/generated/google/apis/securitycenter_v1/service.rb +30 -30
  130. data/generated/google/apis/securitycenter_v1beta1.rb +1 -1
  131. data/generated/google/apis/securitycenter_v1beta1/classes.rb +7 -0
  132. data/generated/google/apis/securitycenter_v1beta1/representations.rb +1 -0
  133. data/generated/google/apis/securitycenter_v1beta2.rb +1 -1
  134. data/generated/google/apis/securitycenter_v1beta2/classes.rb +7 -0
  135. data/generated/google/apis/securitycenter_v1beta2/representations.rb +1 -0
  136. data/generated/google/apis/servicemanagement_v1.rb +1 -1
  137. data/generated/google/apis/servicemanagement_v1/classes.rb +11 -10
  138. data/generated/google/apis/sqladmin_v1beta4.rb +1 -1
  139. data/generated/google/apis/sqladmin_v1beta4/classes.rb +7 -7
  140. data/generated/google/apis/sqladmin_v1beta4/service.rb +4 -1
  141. data/generated/google/apis/storagetransfer_v1.rb +1 -1
  142. data/generated/google/apis/storagetransfer_v1/classes.rb +25 -0
  143. data/generated/google/apis/storagetransfer_v1/representations.rb +3 -0
  144. data/generated/google/apis/webmasters_v3/classes.rb +8 -0
  145. data/generated/google/apis/webmasters_v3/representations.rb +1 -0
  146. data/generated/google/apis/youtube_v3.rb +1 -1
  147. data/generated/google/apis/youtube_v3/classes.rb +18 -143
  148. data/generated/google/apis/youtube_v3/representations.rb +0 -57
  149. data/generated/google/apis/youtube_v3/service.rb +0 -43
  150. data/lib/google/apis/version.rb +1 -23
  151. metadata +27 -263
  152. data/.github/CONTRIBUTING.md +0 -31
  153. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -36
  154. data/.github/ISSUE_TEMPLATE/feature_request.md +0 -21
  155. data/.github/ISSUE_TEMPLATE/support_request.md +0 -7
  156. data/.github/workflows/autoapprove.yml +0 -49
  157. data/.github/workflows/release-please.yml +0 -57
  158. data/.gitignore +0 -23
  159. data/.kokoro/build.bat +0 -11
  160. data/.kokoro/build.sh +0 -4
  161. data/.kokoro/continuous/common.cfg +0 -25
  162. data/.kokoro/continuous/linux.cfg +0 -25
  163. data/.kokoro/continuous/osx.cfg +0 -8
  164. data/.kokoro/continuous/post.cfg +0 -30
  165. data/.kokoro/continuous/windows.cfg +0 -29
  166. data/.kokoro/osx.sh +0 -4
  167. data/.kokoro/presubmit/common.cfg +0 -24
  168. data/.kokoro/presubmit/linux.cfg +0 -24
  169. data/.kokoro/presubmit/osx.cfg +0 -8
  170. data/.kokoro/presubmit/windows.cfg +0 -29
  171. data/.kokoro/release.cfg +0 -94
  172. data/.kokoro/trampoline.bat +0 -10
  173. data/.kokoro/trampoline.sh +0 -4
  174. data/.repo-metadata.json +0 -5
  175. data/.rspec +0 -2
  176. data/.rubocop.yml +0 -12
  177. data/.rubocop_todo.yml +0 -63
  178. data/CODE_OF_CONDUCT.md +0 -43
  179. data/Gemfile +0 -55
  180. data/MIGRATING.md +0 -215
  181. data/README.md +0 -402
  182. data/Rakefile +0 -131
  183. data/api_list_config.yaml +0 -9
  184. data/api_names.yaml +0 -1082
  185. data/bin/generate-api +0 -194
  186. data/docs/api-keys.md +0 -20
  187. data/docs/auth.md +0 -53
  188. data/docs/client-secrets.md +0 -69
  189. data/docs/getting-started.md +0 -340
  190. data/docs/installation.md +0 -0
  191. data/docs/logging.md +0 -34
  192. data/docs/media-upload.md +0 -25
  193. data/docs/oauth-installed.md +0 -191
  194. data/docs/oauth-server.md +0 -133
  195. data/docs/oauth-web.md +0 -280
  196. data/docs/oauth.md +0 -268
  197. data/docs/pagination.md +0 -29
  198. data/docs/performance.md +0 -25
  199. data/google-api-client.gemspec +0 -36
  200. data/lib/google/api_client/auth/installed_app.rb +0 -143
  201. data/lib/google/api_client/auth/key_utils.rb +0 -94
  202. data/lib/google/api_client/auth/storage.rb +0 -104
  203. data/lib/google/api_client/auth/storages/file_store.rb +0 -57
  204. data/lib/google/api_client/auth/storages/redis_store.rb +0 -59
  205. data/lib/google/api_client/client_secrets.rb +0 -176
  206. data/lib/google/apis.rb +0 -58
  207. data/lib/google/apis/core/api_command.rb +0 -209
  208. data/lib/google/apis/core/base_service.rb +0 -447
  209. data/lib/google/apis/core/batch.rb +0 -236
  210. data/lib/google/apis/core/composite_io.rb +0 -97
  211. data/lib/google/apis/core/download.rb +0 -118
  212. data/lib/google/apis/core/hashable.rb +0 -44
  213. data/lib/google/apis/core/http_command.rb +0 -447
  214. data/lib/google/apis/core/json_representation.rb +0 -153
  215. data/lib/google/apis/core/multipart.rb +0 -135
  216. data/lib/google/apis/core/upload.rb +0 -273
  217. data/lib/google/apis/errors.rb +0 -89
  218. data/lib/google/apis/generator.rb +0 -71
  219. data/lib/google/apis/generator/annotator.rb +0 -339
  220. data/lib/google/apis/generator/helpers.rb +0 -76
  221. data/lib/google/apis/generator/model.rb +0 -187
  222. data/lib/google/apis/generator/template.rb +0 -124
  223. data/lib/google/apis/generator/templates/_class.tmpl +0 -43
  224. data/lib/google/apis/generator/templates/_method.tmpl +0 -92
  225. data/lib/google/apis/generator/templates/_representation.tmpl +0 -51
  226. data/lib/google/apis/generator/templates/_representation_stub.tmpl +0 -17
  227. data/lib/google/apis/generator/templates/_representation_type.tmpl +0 -10
  228. data/lib/google/apis/generator/templates/classes.rb.tmpl +0 -29
  229. data/lib/google/apis/generator/templates/module.rb.tmpl +0 -40
  230. data/lib/google/apis/generator/templates/representations.rb.tmpl +0 -32
  231. data/lib/google/apis/generator/templates/service.rb.tmpl +0 -61
  232. data/lib/google/apis/options.rb +0 -116
  233. data/rakelib/devsite/devsite_builder.rb +0 -126
  234. data/rakelib/devsite/link_checker.rb +0 -64
  235. data/rakelib/devsite/repo_metadata.rb +0 -56
  236. data/rakelib/metrics.rake +0 -22
  237. data/rakelib/rubocop.rake +0 -10
  238. data/rakelib/spec.rake +0 -11
  239. data/rakelib/yard.rake +0 -11
  240. data/samples/cli/.env +0 -4
  241. data/samples/cli/Gemfile +0 -7
  242. data/samples/cli/README.md +0 -51
  243. data/samples/cli/google-api-samples +0 -32
  244. data/samples/cli/lib/base_cli.rb +0 -95
  245. data/samples/cli/lib/samples/adsense.rb +0 -99
  246. data/samples/cli/lib/samples/analytics.rb +0 -80
  247. data/samples/cli/lib/samples/androidpublisher.rb +0 -47
  248. data/samples/cli/lib/samples/bigquery.rb +0 -101
  249. data/samples/cli/lib/samples/calendar.rb +0 -83
  250. data/samples/cli/lib/samples/drive.rb +0 -82
  251. data/samples/cli/lib/samples/gmail.rb +0 -157
  252. data/samples/cli/lib/samples/pubsub.rb +0 -114
  253. data/samples/cli/lib/samples/sheets.rb +0 -61
  254. data/samples/cli/lib/samples/translate.rb +0 -44
  255. data/samples/cli/lib/samples/vision.rb +0 -69
  256. data/samples/cli/lib/samples/you_tube.rb +0 -47
  257. data/samples/web/Gemfile +0 -9
  258. data/samples/web/README.md +0 -44
  259. data/samples/web/app.rb +0 -120
  260. data/samples/web/views/calendar.erb +0 -34
  261. data/samples/web/views/drive.erb +0 -33
  262. data/samples/web/views/home.erb +0 -34
  263. data/samples/web/views/layout.erb +0 -40
  264. data/synth.py +0 -40
File without changes
@@ -1,34 +0,0 @@
1
- # Logging
2
-
3
- This page provides logging tips to help you debug your applications.
4
-
5
- ## Accessing the Logger
6
-
7
- Logging is enabled by default in this library using Ruby's standard Logger class.
8
-
9
- You can access the library logger with the logger property of Google::Apis.
10
-
11
- ## Log Level
12
- You can set the logging level to one of the following:
13
-
14
- - FATAL (least amount of logging)
15
- - ERROR
16
- - WARN
17
- - INFO
18
- - DEBUG (most amount of logging)
19
- In the following code, the logging level is set to DEBUG and the Google Plus API is called:
20
-
21
- ```rb
22
- require 'google/apis/plus_v1'
23
-
24
- Google::Apis.logger.level = Logger::DEBUG
25
-
26
- plus = Google::Apis::PlusV1::PlusService.new
27
- activities = plus.list_activities('103354693083460731603', 'public')
28
- ```
29
-
30
- The output of this code should include debug info:
31
-
32
- ```
33
- D, [2015-06-26T14:33:42.583914 #12144] DEBUG -- : Sending HTTP get https://www.googleapis.com/plus/v1/people/103354693083460731603/activities/public?key=...
34
- ```
@@ -1,25 +0,0 @@
1
- ## Media Upload
2
-
3
- For APIs that support file uploads, two additional keyword parameters are available on the method. The parameter upload_source specifies the content to upload while content_type indicates the MIME type. The upload source may be either a file name, IO, or StringIO instance.
4
-
5
- For example, to upload a file named 'mymovie.m4v' to Google Drive:
6
-
7
- ```rb
8
- require 'google/apis/drive_v2'
9
-
10
- drive = Google::Apis::DriveV2:DriveService.new
11
- drive.authorization = ...
12
- drive.insert_file({title: 'My Favorite Movie'}, upload_source: 'mymovie.m4v',
13
- content_type: 'video/mp4')
14
- ```
15
-
16
- ## Resumable media
17
-
18
- For large media files, you can use resumable media uploads to send files, which allows files to be uploaded in smaller chunks. This is especially useful if you are transferring large files, and the likelihood of a network interruption or some other transmission failure is high. It can also reduce your bandwidth usage in the event of network failures because you don't have to restart large file uploads from the beginning.
19
-
20
- To use resumable uploads, enable retries by setting the retry count to any value greater than 0. The client will automatically resume the upload in the event of an error, up to the configured number of retries.:
21
-
22
- ```rb
23
- drive.insert_file(file_metadata, upload_source: 'mymovie.m4v',
24
- content_type: 'video/mp4', options: { retries: 3 } )
25
- ```
@@ -1,191 +0,0 @@
1
- # Using OAuth 2.0 for Installed Applications
2
-
3
- The Google APIs Client Library for Ruby supports using OAuth 2.0 in applications that are installed on a device such as a computer, a cell phone, or a tablet. Installed apps are distributed to individual machines, and it is assumed that these apps cannot keep secrets. These apps might access a Google API while the user is present at the app, or when the app is running in the background.
4
-
5
- This document is for you if:
6
-
7
- - You are writing an installed app for a platform other than Android or iOS, and
8
- - Your installed app will run on devices that have a system browser and rich input capabilities, such as devices with full keyboards.
9
-
10
- If you are writing an app for Android or iOS, use [Google Sign-In](https://developers.google.com/identity) to authenticate your users. The Google Sign-In button manages the OAuth 2.0 flow both for authentication and for obtaining authorization to Google APIs. To add the Google Sign-In button, follow the steps for [Android](https://developers.google.com/identity/sign-in/android) or [iOS](https://developers.google.com/identity/sign-in/ios).
11
-
12
- If your app will run on devices that do not have access to a system browser, or devices with limited input capabilities (for example, if your app will run on game consoles, video cameras, or printers), then see [Using OAuth 2.0 for Devices](https://developers.google.com/accounts/docs/OAuth2ForDevices).
13
-
14
- ## Overview
15
-
16
- To use OAuth 2.0 in a locally-installed application, first create application credentials for your project in the API Console.
17
-
18
- Then, when your application needs to access a user's data with a Google API, your application sends the user to Google's OAuth 2.0 server. The OAuth 2.0 server authenticates the user and obtains consent from the user for your application to access the user's data.
19
-
20
- Next, Google's OAuth 2.0 server sends a single-use authorization code to your application, either in the title bar of the browser or in the query string of an HTTP request to the local host. Your application exchanges this authorization code for an access token.
21
-
22
- Finally, your application can use the access token to call Google APIs.
23
-
24
- This flow is similar to the one shown in the [Using OAuth 2.0 for Web Server Applications](docs/oauth-server.md), but with three differences:
25
-
26
- - When creating a client ID, you specify that your application is an Installed application. This results in a different value for the redirect_uri parameter.
27
- - The client ID and client secret obtained from the API Console are embedded in the source code of your application. In this context, the client secret is obviously not treated as a secret.
28
- - The authorization code can be returned to your application in the title bar of the browser or in the query string of an HTTP request to the local host.
29
-
30
- ## Creating application credentials
31
-
32
- All applications that use OAuth 2.0 must have credentials that identify the application to the OAuth 2.0 server. Applications that have these credentials can access the APIs that you enabled for your project.
33
-
34
- To obtain application credentials for your project, complete these steps:
35
-
36
- 1. Open the [Credentials page](https://console.developers.google.com/apis/credentials) in the API Console.
37
- 1. If you haven't done so already, create your OAuth 2.0 credentials by clicking **Create new Client ID** under the **OAuth** heading and selecting the **Installed application** type. Next, look for your application's client ID and client secret in the relevant table.
38
-
39
- Download the client_secrets.json file and securely store it in a location that only your application can access.
40
-
41
- > **Important:** Do not store the client_secrets.json file in a publicly-accessible location, and if you share the source code to your application—for example, on GitHub—store the client_secrets.json file outside of your source tree to avoid inadvertently sharing your client credentials.
42
-
43
- ## Configuring the client object
44
-
45
- Use the client application credentials that you created to configure a client object in your application. When you configure a client object, you specify the scopes your application needs to access, along with a redirect URI, which will handle the response from the OAuth 2.0 server.
46
-
47
- ### Choosing a redirect URI
48
-
49
- When you create a client ID in the [Google API Console](https://console.developers.google.com/), two redirect_uri parameters are created for you: `urn:ietf:wg:oauth:2.0:oob` and `http://localhost`. The value your application uses determines how the authorization code is returned to your application.
50
-
51
- #### http://localhost
52
-
53
- This value signals to the Google Authorization Server that the authorization code should be returned as a query string parameter to the web server on the client. You can specify a port number without changing the [Google API Console](https://console.developers.google.com/) configuration. To receive the authorization code using this URI, your application must be listening on the local web server. This is possible on many, but not all, platforms. If your platform supports it, this is the recommended mechanism for obtaining the authorization code.
54
-
55
- > **Note:** In some cases, although it is possible to listen, other software (such as a Windows firewall) prevents delivery of the message without significant client configuration.
56
-
57
- #### urn:ietf:wg:oauth:2.0:oob
58
-
59
- This value signals to the Google Authorization Server that the authorization code should be returned in the title bar of the browser, with the page text prompting the user to copy the code and paste it in the application. This is useful when the client (such as a Windows application) cannot listen on an HTTP port without significant client configuration.
60
-
61
- When you use this value, your application can then detect that the page has loaded, and can read the title of the HTML page to obtain the authorization code. It is then up to your application to close the browser window if you want to ensure that the user never sees the page that contains the authorization code. The mechanism for doing this varies from platform to platform.
62
-
63
- If your platform doesn't allow you to detect that the page has loaded or read the title of the page, you can have the user paste the code back to your application, as prompted by the text in the confirmation page that the OAuth 2.0 server generates.
64
-
65
- #### urn:ietf:wg:oauth:2.0:oob:auto
66
-
67
- urn:ietf:wg:oauth:2.0:oob:auto
68
- This is identical to urn:ietf:wg:oauth:2.0:oob, but the text in the confirmation page that the OAuth 2.0 server generates won't instruct the user to copy the authorization code, but instead will simply ask the user to close the window.
69
-
70
- This is useful when your application reads the title of the HTML page (by checking window titles on the desktop, for example) to obtain the authorization code, but can't close the page on its own.
71
-
72
- ### Creating the object
73
-
74
- To create a client object from the client_secrets.json file, use the to_authorization method of a ClientSecrets object. For example, to request read-only access to a user's Google Drive:
75
-
76
- ```rb
77
- require 'google/api_client'
78
-
79
- client_secrets = Google::APIClient::ClientSecrets.load
80
- auth_client = client_secrets.to_authorization
81
- auth_client.update!(
82
- :scope => 'https://www.googleapis.com/auth/drive.metadata.readonly',
83
- :redirect_uri => 'urn:ietf:wg:oauth:2.0:oob'
84
- )
85
- ```
86
-
87
- Your application uses the client object to perform OAuth 2.0 operations, such as generating authorization request URIs and applying access tokens to HTTP requests.
88
-
89
- ## Sending users to Google's OAuth 2.0 server
90
-
91
- When your application needs to access a user's data, redirect the user to Google's OAuth 2.0 server.
92
-
93
- 1. Generate a URL to request access from Google's OAuth 2.0 server:
94
-
95
- `auth_uri = auth_client.authorization_uri.to_s`
96
-
97
- 2. Open auth_uri in a browser:
98
-
99
- ```rb
100
- require 'launchy'
101
-
102
- Launchy.open(auth_uri)
103
- ```
104
-
105
- Google's OAuth 2.0 server will authenticate the user and obtain consent from the user for your application to access the requested scopes. The response will be sent back to your application using the redirect URI specified in the client object.
106
-
107
- ## Handling the OAuth 2.0 server response
108
-
109
- The OAuth 2.0 server responds to your application's access request by using the URI specified in the request.
110
-
111
- If the user approves the access request, then the response contains an authorization code. If the user does not approve the request, the response contains an error message. Depending on the redirect URI that you specified, the response is in the query string of an HTTP request to the local host, or in a web page, from which the user can copy and paste the authorization code.
112
-
113
- To exchange an authorization code for an access token, use the fetch_access_token! method:
114
-
115
- ```rb
116
- auth_client.code = auth_code
117
- auth_client.fetch_access_token!
118
- ```
119
-
120
- ## Calling Google APIs
121
-
122
- Use the auth_client object to call Google APIs by completing the following steps:
123
-
124
- 1. Build a service object for the API that you want to call. You build a a service object by calling the discovered_api function with the name and version of the API. For example, to call version 2 of the Drive API:
125
-
126
- ```rb
127
- require 'google/apis/drive_v2'
128
- drive = Google::Apis::DriveV2::DriveService.new
129
- drive.authorization = auth_client
130
- ```
131
-
132
- 2. Make requests to the API service using the interface provided by the service object. For example, to list the files in the authenticated user's Google Drive:
133
-
134
- ```rb
135
- files = drive.list_files()
136
- ```
137
-
138
- ## Complete example
139
-
140
- The following example prints a JSON-formatted list of files in a user's Google Drive after the user authenticates and gives consent for the application to access the user's Drive files.
141
-
142
- ```rb
143
- require 'google/apis/drive_v2'
144
- require 'google/api_client/client_secrets'
145
- require 'launchy'
146
-
147
- client_secrets = Google::APIClient::ClientSecrets.load
148
- auth_client = client_secrets.to_authorization
149
- auth_client.update!(
150
- :scope => 'https://www.googleapis.com/auth/drive.metadata.readonly',
151
- :redirect_uri => 'urn:ietf:wg:oauth:2.0:oob'
152
- )
153
-
154
- auth_uri = auth_client.authorization_uri.to_s
155
- Launchy.open(auth_uri)
156
-
157
- puts 'Paste the code from the auth response page:'
158
- auth_client.code = gets
159
- auth_client.fetch_access_token!
160
-
161
- drive = Google::Apis::DriveV2::DriveService.new
162
- drive.authorization = auth_client
163
- files = drive.list_files
164
-
165
- files.items.each do |file|
166
- puts file.title
167
- end
168
- ```
169
-
170
- If you want to use a local web server to handle the OAuth 2.0 response, you can use the InstalledAppFlow helper to simplify the process. For example:
171
-
172
- ```rb
173
- require 'google/apis/drive_v2'
174
- require 'google/api_client/client_secrets'
175
- require 'google/api_client/auth/installed_app'
176
-
177
- client_secrets = Google::APIClient::ClientSecrets.load
178
- flow = Google::APIClient::InstalledAppFlow.new(
179
- :client_id => client_secrets.client_id,
180
- :client_secret => client_secrets.client_secret,
181
- :scope => 'https://www.googleapis.com/auth/drive.metadata.readonly',
182
- :port => 5000)
183
-
184
- drive = Google::Apis::DriveV2::DriveService.new
185
- drive.authorization = flow.authorize
186
- files = drive.list_files
187
-
188
- files.items.each do |file|
189
- puts file.title
190
- end
191
- ```
@@ -1,133 +0,0 @@
1
- # Using OAuth 2.0 for Server to Server Applications
2
-
3
- The Google APIs Client Library for Ruby supports using OAuth 2.0 for server-to-server interactions such as those between a web application and a Google service. For this scenario you need a service account, which is an account that belongs to your application instead of to an individual end user. Your application calls Google APIs on behalf of the service account, so users aren't directly involved. This scenario is sometimes called "two-legged OAuth," or "2LO." (The related term "three-legged OAuth" refers to scenarios in which your application calls Google APIs on behalf of end users, and in which user consent is sometimes required.)
4
-
5
- Typically, an application uses a service account when the application uses Google APIs to work with its own data rather than a user's data. For example, an application that uses [Google Cloud Datastore](https://cloud.google.com/datastore/) for data persistence would use a service account to authenticate its calls to the Google Cloud Datastore API.
6
-
7
- If you have a G Suite domain—if you use [G Suite](https://gsuite.google.com/), for example—an administrator of the G Suite domain can authorize an application to access user data on behalf of users in the G Suite domain. For example, an application that uses the [Google Calendar API](https://developers.google.com/calendar/) to add events to the calendars of all users in a G Suite domain would use a service account to access the Google Calendar API on behalf of users. Authorizing a service account to access data on behalf of users in a domain is sometimes referred to as "delegating domain-wide authority" to a service account.
8
-
9
- > **Note:** When you use [G Suite Marketplace](https://www.google.com/enterprise/marketplace/) to install an application for your domain, the required permissions are automatically granted to the application. You do not need to manually authorize the service accounts that the application uses.
10
-
11
- > **Note:** Although you can use service accounts in applications that run from a Google Apps domain, service accounts are not members of your Google Apps account and aren't subject to domain policies set by Google Apps administrators. For example, a policy set in the Google Apps admin console to restrict the ability of Apps end users to share documents outside of the domain would not apply to service accounts.
12
-
13
- This document describes how an application can complete the server-to-server OAuth 2.0 flow by using the Google APIs Client Library for Ruby.
14
-
15
- ## Overview
16
-
17
- To support server-to-server interactions, first create a service account for your project in the API Console. If you want to access user data for users in your Google Apps domain, then delegate domain-wide access to the service account.
18
-
19
- Then, your application prepares to make authorized API calls by using the service account's credentials to request an access token from the OAuth 2.0 auth server.
20
-
21
- Finally, your application can use the access token to call Google APIs.
22
-
23
- ## Creating a service account
24
-
25
- A service account's credentials include a generated email address that is unique, a client ID, and at least one public/private key pair.
26
-
27
- If your application runs on Google App Engine, a service account is set up automatically when you create your project.
28
-
29
- If your application runs on Google Compute Engine, a service account is also set up automatically when you create your project, but you must specify the scopes that your application needs access to when you create a Google Compute Engine instance. For more information, see [Preparing an instance to use service accounts](https://cloud.google.com/compute/docs/authentication#using).
30
-
31
- If your application doesn't run on Google App Engine or Google Compute Engine, you must obtain these credentials in the Google API Console. To generate service-account credentials, or to view the public credentials that you've already generated, do the following:
32
-
33
- 1. Open the [**Service accounts** page](https://console.developers.google.com/permissions/serviceaccounts). If prompted, select a project.
34
- 1. Click **Create service account**.
35
- 1. In the **Create service account** window, type a name for the service account, and select **Furnish a new private key**. If you want to [grant G Suite domain-wide authority](https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority) to the service account, also select **Enable G Suite Domain-wide Delegation**. Then click **Create**.
36
-
37
- Your new public/private key pair is generated and downloaded to your machine; it serves as the only copy of this key. You are responsible for storing it securely.
38
-
39
- You can return to the [API Console](https://console.developers.google.com/) at any time to view the client ID, email address, and public key fingerprints, or to generate additional public/private key pairs. For more details about service account credentials in the API Console, see [Service accounts](https://developers.google.com/console/help/service-accounts) in the API Console help file.
40
-
41
- Take note of the service account's email address and store the service account's private key file in a location accessible to your application. Your application needs them to make authorized API calls.
42
-
43
- > **Note:** You must store and manage private keys securely in both development and production environments. Google does not keep a copy of your private keys, only your public keys.
44
-
45
- ## Delegating domain-wide authority to the service account
46
-
47
- If your application runs in a Google Apps domain and accesses user data, the service account that you created needs to be granted access to the user data that you want to access.
48
-
49
- The following steps must be performed by an administrator of the Google Apps domain:
50
-
51
- 1. Go to your Google Apps domain’s [Admin console](http://admin.google.com/).
52
- 1. Select **Security** from the list of controls. If you don't see **Security** listed, select **More controls** from the gray bar at the bottom of the page, then select **Security** from the list of controls. If you can't see the controls, make sure you're signed in as an administrator for the domain.
53
- 1. Select **Advanced settings** from the list of options.
54
- 1. Select **Manage third party OAuth Client access** in the **Authentication** section.
55
- 1. In the **Client name** field enter the service account's **Client ID**.
56
- 1. In the **One or More API Scopes** field enter the list of scopes that your application should be granted access to. For example, if your application needs domain-wide access to the Google Drive API and the Google Calendar API, enter: `https://www.googleapis.com/auth/drive`, `https://www.googleapis.com/auth/calendar`.
57
- 1. Click **Authorize**.
58
-
59
- Your application now has the authority to make API calls as users in your domain (to "impersonate" users). When you prepare to make authorized API calls, you specify the user to impersonate.
60
-
61
- ## Preparing to make an authorized API call
62
-
63
- After you obtain the client email address and private key from the API Console, complete the following steps:
64
-
65
- 1. Create a Client object from the service account's credentials and the scopes your application needs access to. For example:
66
-
67
- ```rb
68
- require 'googleauth'
69
- require 'google/apis/compute_v1'
70
-
71
- compute = Google::Apis::ComputeV1::ComputeService.new
72
-
73
- # Get the environment configured authorization
74
- scopes = ['https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/compute']
75
- compute.authorization = Google::Auth.get_application_default(scopes)
76
- ```
77
-
78
- If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account in the :sub parameter. For example:
79
-
80
- ```rb
81
- require 'googleauth'
82
- require 'google/apis/sqladmin_v1beta4'
83
-
84
- # Get the environment configured authorization
85
- scopes = ['https://www.googleapis.com/auth/sqlservice.admin']
86
- authorization = Google::Auth.get_application_default(scopes)
87
-
88
- # Clone and set the subject
89
- auth_client = authorization.dup
90
- auth_client.sub = 'user@example.org'
91
- ```
92
-
93
- 2. Use the fetch_access_token! method of the client object to acquire an access token. For example:
94
-
95
- `auth_client.fetch_access_token!`
96
-
97
- Use the authorized Client object to call Google APIs in your application.
98
-
99
- ## Calling Google APIs
100
-
101
- Use the authorized Client object to call Google APIs by completing the following steps:
102
-
103
- 1. Build a service object for the API that you want to call. You build a a service object by calling the discovered_api method of an APIClient object with the name and version of the API. For example, to call version 1beta3 of the Cloud SQL Administration API:
104
-
105
- ```rb
106
- sqladmin = Google::Apis::SqladminV1beta4::SqladminService.new
107
- sqladmin.authorization = auth_client
108
-
109
- # Make requests to the API service using the interface
110
- # provided by the service object, and providing the authorized
111
- # Client object. For example, to list the instances of Cloud SQL
112
- # databases in the examinable-example-123 project:
113
-
114
- instances = sqladmin.list_instances('examinable-example-123')
115
- ```
116
-
117
- ## Complete example
118
-
119
- The following example prints a JSON-formatted list of Cloud SQL instances in a project.
120
-
121
- ```rb
122
- require 'googleauth'
123
- require 'google/apis/sqladmin_v1beta4'
124
-
125
- sqladmin = Google::Apis::SqladminV1beta4::SqladminService.new
126
-
127
- # Get the environment configured authorization
128
- scopes = ['https://www.googleapis.com/auth/sqlservice.admin']
129
- sqladmin.authorization = Google::Auth.get_application_default(scopes)
130
-
131
- instances = sqladmin.list_instances('examinable-example-123')
132
- puts instances.to_h
133
- ```
@@ -1,280 +0,0 @@
1
- # Using OAuth 2.0 for Web Server Applications
2
-
3
- This document explains how web server applications use the Google API Client Library for Ruby to implement OAuth 2.0 authorization to access Google APIs. OAuth 2.0 allows users to share specific data with an application while keeping their usernames, passwords, and other information private. For example, an application can use OAuth 2.0 to obtain permission from users to store files in their Google Drives.
4
-
5
- This OAuth 2.0 flow is specifically for user authorization. It is designed for applications that can store confidential information and maintain state. A properly authorized web server application can access an API while the user interacts with the application or after the user has left the application.
6
-
7
- Web server applications frequently also use [service accounts](service-accounts.md) to authorize API requests, particularly when calling Cloud APIs to access project-based data rather than user-specific data. Web server applications can use service accounts in conjunction with user authorization.
8
-
9
- ## Prerequisites
10
-
11
- ### Enable APIs for your project
12
-
13
- Any application that calls Google APIs needs to enable those APIs in the API Console. To enable the appropriate APIs for your project:
14
-
15
- 1. Open the [Library](https://console.developers.google.com/apis/library) page in the API Console.
16
- 1. Select the project associated with your application. Create a project if you do not have one already.
17
- 1. Use the **Library** page to find each API that your application will use. Click on each API and enable it for your project.
18
-
19
- ### Create authorization credentials
20
-
21
- Any application that uses OAuth 2.0 to access Google APIs must have authorization credentials that identify the application to Google's OAuth 2.0 server. The following steps explain how to create credentials for your project. Your applications can then use the credentials to access APIs that you have enabled for that project.
22
-
23
- <ol>
24
- <li>Open the <a href="https://console.developers.google.com/apis/credentials">Credentials page</a> in the API Console.</li>
25
-
26
- <li>Click <b>Create credentials &gt; OAuth client ID</b>.</li>
27
- <li>Complete the form. Set the application type to <code>Web
28
- application</code>. Applications that use languages and frameworks
29
- like PHP, Java, Python, Ruby, and .NET must specify authorized
30
- <b>redirect URIs</b>. The redirect URIs are the endpoints to which the
31
- OAuth 2.0 server can send responses.<br><br>
32
- For testing, you can specify URIs that refer to the local machine,
33
- such as <code>http://localhost:8080</code>. With that in mind, please
34
- note that all of the examples in this document use
35
- <code>http://localhost:8080</code> as the redirect URI.
36
- <br><br>
37
- We recommend that you <a href="#protectauthcode">design your app's auth
38
- endpoints</a> so that your application does not expose authorization
39
- codes to other resources on the page.</li>
40
- </ol>
41
-
42
- After creating your credentials, download the **client_secret.json** file from the API Console. Securely store the file in a location that only your application can access.
43
-
44
- > **Important:** Do not store the **client_secret.json** file in a publicly-accessible location. In addition, if you share the source code to your application—for example, on GitHub—store the **client_secret.json** file outside of your source tree to avoid inadvertently sharing your client credentials.
45
-
46
- ### Identify access scopes
47
-
48
- Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there may be an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent.
49
-
50
- Before you start implementing OAuth 2.0 authorization, we recommend that you identify the scopes that your app will need permission to access.
51
-
52
- We also recommend that your application request access to authorization scopes via an incremental authorization process, in which your application requests access to user data in context. This best practice helps users to more easily understand why your application needs the access it is requesting.
53
-
54
- The [OAuth 2.0 API Scopes document](https://developers.google.com/identity/protocols/googlescopes) contains a full list of scopes that you might use to access Google APIs.
55
-
56
- ## Obtaining OAuth 2.0 access tokens
57
-
58
- The following steps show how your application interacts with Google's OAuth 2.0 server to obtain a user's consent to perform an API request on the user's behalf. Your application must have that consent before it can execute a Google API request that requires user authorization.
59
-
60
- ### Step 1: Configure the client object
61
-
62
- Your first step is to configure the client object, which your application uses to obtain user authorization and to make authorized API requests.
63
-
64
- The client object identifies the scopes that your application is requesting permission to access. These values inform the consent screen that Google displays to the user. The Choosing access scopes section provides information about how to determine which scopes your application should request permission to access.
65
-
66
- Use the client_secrets.json file that you created to configure a client object in your application. When you configure a client object, you specify the scopes your application needs to access, along with the URL to your application's auth endpoint, which will handle the response from the OAuth 2.0 server.
67
-
68
- For example, to request read-only, offline access to a user's Google Drive:
69
-
70
- ```rb
71
- require 'google/apis/drive_v2'
72
- require 'google/api_client/client_secrets'
73
-
74
- client_secrets = Google::APIClient::ClientSecrets.load
75
- auth_client = client_secrets.to_authorization
76
- auth_client.update!(
77
- :scope => 'https://www.googleapis.com/auth/drive.metadata.readonly',
78
- :redirect_uri => 'http://www.example.com/oauth2callback',
79
- :additional_parameters => {
80
- "access_type" => "offline", # offline access
81
- "include_granted_scopes" => "true" # incremental auth
82
- }
83
- )
84
- ```
85
-
86
- Your application uses the client object to perform OAuth 2.0 operations, such as generating authorization request URLs and applying access tokens to HTTP requests.
87
-
88
- ### Step 2: Redirect to Google's OAuth 2.0 server
89
-
90
- When your application needs to access a user's data, redirect the user to Google's OAuth 2.0 server.
91
-
92
- 1. Generate a URL to request access from Google's OAuth 2.0 server:
93
-
94
- ```rb
95
- auth_uri = auth_client.authorization_uri.to_s
96
- ```
97
-
98
- 2. Redirect the user to auth_uri.
99
-
100
- Google's OAuth 2.0 server authenticates the user and obtains consent from the user for your application to access the requested scopes. The response is sent back to your application using the redirect URL you specified.
101
-
102
- ### Step 3: Google prompts user for consent
103
-
104
- In this step, the user decides whether to grant your application the requested access. At this stage, Google displays a consent window that shows the name of your application and the Google API services that it is requesting permission to access with the user's authorization credentials. The user can then consent or refuse to grant access to your application.
105
-
106
- Your application doesn't need to do anything at this stage as it waits for the response from Google's OAuth 2.0 server indicating whether the access was granted. That response is explained in the following step.
107
-
108
- ### Step 4: Handle the OAuth 2.0 server response
109
-
110
- The OAuth 2.0 server responds to your application's access request by using the URL specified in the request.
111
-
112
- If the user approves the access request, then the response contains an authorization code. If the user does not approve the request, the response contains an error message. The authorization code or error message that is returned to the web server appears on the query string, as shown below:
113
-
114
- An error response:
115
-
116
- ```
117
- https://oauth2.example.com/auth?error=access_denied
118
- ```
119
-
120
- An authorization code response:
121
-
122
- ```
123
- https://oauth2.example.com/auth?code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7
124
- ```
125
-
126
- > Important: If your response endpoint renders an HTML page, any resources on that page will be able to see the authorization code in the URL. Scripts can read the URL directly, and the URL in the Referer HTTP header may be sent to any or all resources on the page.
127
- >
128
- > Carefully consider whether you want to send authorization credentials to all resources on that page (especially third-party scripts such as social plugins and analytics). To avoid this issue, we recommend that the server first handle the request, then redirect to another URL that doesn't include the response parameters.
129
-
130
- **Sample OAuth 2.0 server response**
131
-
132
- You can test this flow by clicking on the following sample URL, which requests read-only access to view metadata for files in your Google Drive:
133
-
134
- ```
135
- https://accounts.google.com/o/oauth2/v2/auth?
136
- scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.metadata.readonly&
137
- access_type=offline&
138
- include_granted_scopes=true&
139
- state=state_parameter_passthrough_value&
140
- redirect_uri=http%3A%2F%2Foauth2.example.com%2Fcallback&
141
- response_type=code&
142
- client_id=client_id
143
- ```
144
-
145
- After completing the OAuth 2.0 flow, you should be redirected to http://localhost/oauth2callback, which will likely yield a 404 NOT FOUND error unless your local machine serves a file at that address. The next step provides more detail about the information returned in the URI when the user is redirected back to your application.
146
-
147
- ### Step 5: Exchange authorization code for refresh and access tokens
148
-
149
- After the web server receives the authorization code, it can exchange the authorization code for an access token.
150
-
151
- To exchange an authorization code for an access token, use the fetch_access_token! method:
152
-
153
- ```rb
154
- auth_client.code = auth_code
155
- auth_client.fetch_access_token!
156
- ```
157
-
158
- ## Calling Google APIs
159
-
160
- Use the auth_client object to call Google APIs by completing the following steps:
161
-
162
- 1. Build a service object for the API that you want to call. For example, to call version 2 of the Drive API:
163
- `drive = Google::Apis::DriveV2::DriveService.new`
164
- 2. Set the credentials on the service:
165
- `drive.authorization = auth_client`
166
- 3. Make requests to the API service using the interface provided by the service object. For example, to list the files in the authenticated user's Google Drive:
167
- `files = drive.list_files`
168
-
169
- Alternately, authorization can be provided on a per-method basis by supplying the options parameter to a method:
170
-
171
- `files = drive.list_files(options: { authorization: auth_client })`
172
-
173
- ## Complete example
174
-
175
- The following example prints a JSON-formatted list of files in a user's Google Drive after the user authenticates and gives consent for the application to access the user's Drive files.
176
-
177
- This example uses the Sinatra framework.
178
-
179
- ```rb
180
- require 'google/apis/drive_v2'
181
- require 'google/api_client/client_secrets'
182
- require 'json'
183
- require 'sinatra'
184
-
185
- enable :sessions
186
- set :session_secret, 'setme'
187
-
188
- get '/' do
189
- unless session.has_key?(:credentials)
190
- redirect to('/oauth2callback')
191
- end
192
- client_opts = JSON.parse(session[:credentials])
193
- auth_client = Signet::OAuth2::Client.new(client_opts)
194
- drive = Google::Apis::DriveV2::DriveService.new
195
- files = drive.list_files(options: { authorization: auth_client })
196
- "<pre>#{JSON.pretty_generate(files.to_h)}</pre>"
197
- end
198
-
199
- get '/oauth2callback' do
200
- client_secrets = Google::APIClient::ClientSecrets.load
201
- auth_client = client_secrets.to_authorization
202
- auth_client.update!(
203
- :scope => 'https://www.googleapis.com/auth/drive.metadata.readonly',
204
- :redirect_uri => url('/oauth2callback'))
205
- if request['code'] == nil
206
- auth_uri = auth_client.authorization_uri.to_s
207
- redirect to(auth_uri)
208
- else
209
- auth_client.code = request['code']
210
- auth_client.fetch_access_token!
211
- auth_client.client_secret = nil
212
- session[:credentials] = auth_client.to_json
213
- redirect to('/')
214
- end
215
- end
216
- ```
217
-
218
- ## Incremental authorization
219
-
220
- In the OAuth 2.0 protocol, your app requests authorization to access resources, which are identified by scopes. It is considered a best user-experience practice to request authorization for resources at the time you need them. To enable that practice, Google's authorization server supports incremental authorization. This feature lets you request scopes as they are needed and, if the user grants permission, add those scopes to your existing access token for that user.
221
-
222
- For example, an app that lets people sample music tracks and create mixes might need very few resources at sign-in time, perhaps nothing more than the name of the person signing in. However, saving a completed mix would require access to their Google Drive. Most people would find it natural if they only were asked for access to their Google Drive at the time the app actually needed it.
223
-
224
- In this case, at sign-in time the app might request the profile scope to perform basic sign-in, and then later request the https://www.googleapis.com/auth/drive.file scope at the time of the first request to save a mix.
225
-
226
- To implement incremental authorization, you complete the normal flow for requesting an access token but make sure that the authorization request includes previously granted scopes. This approach allows your app to avoid having to manage multiple access tokens.
227
-
228
- The following rules apply to an access token obtained from an incremental authorization:
229
-
230
- The token can be used to access resources corresponding to any of the scopes rolled into the new, combined authorization.
231
- When you use the refresh token for the combined authorization to obtain an access token, the access token represents the combined authorization and can be used for any of its scopes.
232
- The combined authorization includes all scopes that the user granted to the API project even if the grants were requested from different clients. For example, if a user granted access to one scope using an application's desktop client and then granted another scope to the same application via a mobile client, the combined authorization would include both scopes.
233
- If you revoke a token that represents a combined authorization, access to all of that authorization's scopes on behalf of the associated user are revoked simultaneously.
234
- The example for configuring the client object demonstrates how to ensure authorization requests follow this best practice. The code snippet below also shows the code that you need to add to use incremental authorization.
235
-
236
- ```rb
237
- auth_client.update!(
238
- :additional_parameters => {"include_granted_scopes" => "true"}
239
- )
240
- ```
241
-
242
- ## Refreshing an access token (offline access)
243
-
244
- Access tokens periodically expire. You can refresh an access token without prompting the user for permission (including when the user is not present) if you requested offline access to the scopes associated with the token.
245
-
246
- If you use a Google API Client Library, the client object refreshes the access token as needed as long as you configure that object for offline access.
247
-
248
- Requesting offline access is a requirement for any application that needs to access a Google API when the user is not present. For example, an app that performs backup services or executes actions at predetermined times needs to be able to refresh its access token when the user is not present. The default style of access is called online.
249
-
250
- Server-side web applications, installed applications, and devices all obtain refresh tokens during the authorization process. Refresh tokens are not typically used in client-side (JavaScript) web applications.
251
-
252
- If your application needs offline access to a Google API, set the API client's access type to offline:
253
-
254
- ```rb
255
- auth_client.update!(
256
- :additional_parameters => {"access_type" => "offline"}
257
- )
258
- ```
259
-
260
- After a user grants offline access to the requested scopes, you can continue to use the API client to access Google APIs on the user's behalf when the user is offline. The client object will refresh the access token as needed.
261
-
262
- ## Revoking a token
263
-
264
- In some cases a user may wish to revoke access given to an application. A user can revoke access by visiting Account Settings. It is also possible for an application to programmatically revoke the access given to it. Programmatic revocation is important in instances where a user unsubscribes or removes an application. In other words, part of the removal process can include an API request to ensure the permissions granted to the application are removed.
265
-
266
- To programmatically revoke a token, make an HTTP request to the oauth2.revoke endpoint:
267
-
268
- ```rb
269
- uri = URI('https://accounts.google.com/o/oauth2/revoke')
270
- params = { :token => auth_client.access_token }
271
- uri.query = URI.encode_www_form(params)
272
- response = Net::HTTP.get(uri)
273
- ```
274
-
275
- The token can be an access token or a refresh token. If the token is an access token and it has a corresponding refresh token, the refresh token will also be revoked.
276
-
277
- If the revocation is successfully processed, then the status code of the response is 200. For error conditions, a status code 400 is returned along with an error code.
278
-
279
- > Note: Following a successful revocation response, it might take some time before the revocation has full effect.
280
-