@@ -38,23 +38,31 @@ that runs in browser.
38
38
For instructions on setting up your credentials properly, see the
39
39
[ API Console Help] [ console-help ] .
40
40
41
- ## Credential
41
+ ## Credentials
42
42
43
- ### GoogleCredential
43
+ ### GoogleCredentials
44
44
45
- [ ` GoogleCredential ` ] [ google-credential ] is a thread-safe helper class for OAuth
45
+ [ ` GoogleCredentials ` ] [ google-credentials ] is a thread-safe helper class for OAuth
46
46
2.0 for accessing protected resources using an access token. For example, if you
47
47
already have an access token, you can make a request in the following way:
48
48
49
49
``` java
50
- HttpTransport httpTransport = GoogleNetHttpTransport . newTrustedTransport();
51
- JsonFactory jsonFactory = JacksonFactory . getDefaultInstance();
52
-
53
- GoogleCredentials googleCredentials = GoogleCredentials . create(access_token);
54
- HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter (googleCredentials);
55
-
56
- Storage storage = new Storage .Builder (httpTransport, jsonFactory, requestInitializer)
57
- .setApplicationName(" MyProject-1234" )
50
+ import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport ;
51
+ import com.google.api.client.json.gson.GsonFactory ;
52
+ import com.google.api.services.books.Books ;
53
+ import com.google.auth.http.HttpCredentialsAdapter ;
54
+ import com.google.auth.oauth2.AccessToken ;
55
+ import com.google.auth.oauth2.GoogleCredentials ;
56
+
57
+ GoogleCredentials credentials =
58
+ GoogleCredentials . newBuilder(). setAccessToken(new AccessToken (" token" , null )). build();
59
+
60
+ Books books =
61
+ new Books .Builder (
62
+ GoogleNetHttpTransport . newTrustedTransport(),
63
+ GsonFactory . getDefaultInstance(),
64
+ new HttpCredentialsAdapter (credentials))
65
+ .setApplicationName(" BooksExample/1.0" )
58
66
.build();
59
67
```
60
68
@@ -70,24 +78,38 @@ Use [`AppIdentityCredential`][app-identity-credential] (from
70
78
App Engine takes care of all of the details. You only specify the OAuth 2.0
71
79
scope you need.
72
80
73
- Example code taken from [ urlshortener-robots-appengine-sample] [ urlshortener-sample ] :
74
-
75
81
``` java
76
- static Urlshortener newUrlshortener() {
77
- AppIdentityCredential credential =
78
- new AppIdentityCredential (
79
- Collections . singletonList(UrlshortenerScopes . URLSHORTENER ));
80
- return new Urlshortener .Builder (new UrlFetchTransport (),
81
- JacksonFactory . getDefaultInstance(),
82
- credential)
83
- .build();
84
- }
82
+ import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport ;
83
+ import com.google.api.client.json.gson.GsonFactory ;
84
+ import com.google.api.services.books.Books ;
85
+ import com.google.appengine.api.appidentity.AppIdentityService ;
86
+ import com.google.appengine.api.appidentity.AppIdentityServiceFactory ;
87
+ import com.google.auth.appengine.AppEngineCredentials ;
88
+ import com.google.auth.http.HttpCredentialsAdapter ;
89
+ import com.google.auth.oauth2.GoogleCredentials ;
90
+ import java.util.Arrays ;
91
+
92
+ AppIdentityService appIdentityService = AppIdentityServiceFactory . getAppIdentityService();
93
+
94
+ GoogleCredentials credentials =
95
+ AppEngineCredentials . newBuilder()
96
+ .setScopes(Arrays . asList(" scope1" , " scope2" , " scope3" ))
97
+ .setAppIdentityService(appIdentityService)
98
+ .build();
99
+
100
+ Books books =
101
+ new Books .Builder (
102
+ GoogleNetHttpTransport . newTrustedTransport(),
103
+ GsonFactory . getDefaultInstance(),
104
+ new HttpCredentialsAdapter (credentials))
105
+ .setApplicationName(" BooksExample/1.0" )
106
+ .build();
85
107
```
86
108
87
109
## Data store
88
110
89
111
An access token typically has an expiration date of 1 hour, after which you will
90
- get an error if you try to use it. [ GoogleCredential ] [ google-credential ] takes
112
+ get an error if you try to use it. [ GoogleCredentials ] [ google-credentials ] takes
91
113
care of automatically "refreshing" the token, which simply means getting a new
92
114
access token. This is done by means of a long-lived refresh token, which is
93
115
typically received along with the access token if you use the
@@ -201,7 +223,7 @@ public class CalendarServletSample extends AbstractAuthorizationCodeServlet {
201
223
@Override
202
224
protected AuthorizationCodeFlow initializeFlow () throws IOException {
203
225
return new GoogleAuthorizationCodeFlow .Builder (
204
- new NetHttpTransport (), JacksonFactory . getDefaultInstance(),
226
+ new NetHttpTransport (), GsonFactory . getDefaultInstance(),
205
227
" [[ENTER YOUR CLIENT ID]]" , " [[ENTER YOUR CLIENT SECRET]]" ,
206
228
Collections . singleton(CalendarScopes . CALENDAR )). setDataStoreFactory(
207
229
DATA_STORE_FACTORY ). setAccessType(" offline" ). build();
@@ -238,7 +260,7 @@ public class CalendarServletCallbackSample extends AbstractAuthorizationCodeCall
238
260
@Override
239
261
protected AuthorizationCodeFlow initializeFlow () throws IOException {
240
262
return new GoogleAuthorizationCodeFlow .Builder (
241
- new NetHttpTransport (), JacksonFactory . getDefaultInstance()
263
+ new NetHttpTransport (), GsonFactory . getDefaultInstance()
242
264
" [[ENTER YOUR CLIENT ID]]" , " [[ENTER YOUR CLIENT SECRET]]" ,
243
265
Collections . singleton(CalendarScopes . CALENDAR )). setDataStoreFactory(
244
266
DATA_STORE_FACTORY ). setAccessType(" offline" ). build();
@@ -342,7 +364,7 @@ For an additional sample, see
342
364
343
365
### Service accounts
344
366
345
- [ GoogleCredential ] [ google-credential ] also supports [ service accounts] [ service-accounts ] .
367
+ [ GoogleCredentials ] [ google-credentials ] also supports [ service accounts] [ service-accounts ] .
346
368
Unlike the credential in which a client application requests access to an
347
369
end-user's data, Service Accounts provide access to the client application's
348
370
own data. Your client application signs the request for an access token using
@@ -352,7 +374,7 @@ For example, you can make a request in the following way:
352
374
353
375
``` java
354
376
HttpTransport httpTransport = GoogleNetHttpTransport . newTrustedTransport();
355
- JsonFactory jsonFactory = JacksonFactory . getDefaultInstance();
377
+ JsonFactory jsonFactory = GsonFactory . getDefaultInstance();
356
378
357
379
// Build service account credential
358
380
GoogleCredentials googleCredentials = GoogleCredentials .
@@ -530,7 +552,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
530
552
}
531
553
```
532
554
533
- [google-credential ]: https://googleapis.dev/java/google-auth-library/latest/com/google/auth/oauth2/GoogleCredentials.html
555
+ [google-credentials ]: https://googleapis.dev/java/google-auth-library/latest/index.html? com/google/auth/oauth2/GoogleCredentials.html
534
556
[google-oauth-client-instructions]: https://developers.google.com/api-client-library/java/google-oauth-java-client/oauth2
535
557
[oauth2]: https://developers.google.com/accounts/docs/OAuth2
536
558
[javadoc-oauth2]: https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/auth/oauth2/package-frame.html
@@ -539,7 +561,6 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
539
561
[console-help]: https://developer.google.com/console/help/console/
540
562
[identity-api]: https://cloud.google.com/appengine/docs/java/appidentity/?csw=1#Asserting_Identity_to_Google_APIs
541
563
[app-identity-credential]: https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/extensions/appengine/auth/oauth2/AppIdentityCredential.html
542
- [urlshortener-sample]: https://github.com/google/google-api-java-client-samples/tree/master/urlshortener-robots-appengine-sample
543
564
[auth-code-flow-set-access-type]: https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/auth/oauth2/GoogleAuthorizationCodeFlow.Builder.html#setAccessType-java.lang.String-
544
565
[data-store-factory]: https://googleapis.dev/java/google-http-client/latest/com/google/api/client/util/store/DataStoreFactory.html
545
566
[stored-credential]: https://googleapis.dev/java/google-oauth-client/latest/com/google/api/client/auth/oauth2/StoredCredential.html
0 commit comments