@@ -38,23 +38,31 @@ that runs in browser.
3838For instructions on setting up your credentials properly, see the
3939[ API Console Help] [ console-help ] .
4040
41- ## Credential
41+ ## Credentials
4242
43- ### GoogleCredential
43+ ### GoogleCredentials
4444
45- [ ` GoogleCredential ` ] [ google-credential ] is a thread-safe helper class for OAuth
45+ [ ` GoogleCredentials ` ] [ google-credentials ] is a thread-safe helper class for OAuth
46462.0 for accessing protected resources using an access token. For example, if you
4747already have an access token, you can make a request in the following way:
4848
4949``` 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" )
5866 .build();
5967```
6068
@@ -70,24 +78,38 @@ Use [`AppIdentityCredential`][app-identity-credential] (from
7078App Engine takes care of all of the details. You only specify the OAuth 2.0
7179scope you need.
7280
73- Example code taken from [ urlshortener-robots-appengine-sample] [ urlshortener-sample ] :
74-
7581``` 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();
85107```
86108
87109## Data store
88110
89111An 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
91113care of automatically "refreshing" the token, which simply means getting a new
92114access token. This is done by means of a long-lived refresh token, which is
93115typically received along with the access token if you use the
@@ -201,7 +223,7 @@ public class CalendarServletSample extends AbstractAuthorizationCodeServlet {
201223 @Override
202224 protected AuthorizationCodeFlow initializeFlow () throws IOException {
203225 return new GoogleAuthorizationCodeFlow .Builder (
204- new NetHttpTransport (), JacksonFactory . getDefaultInstance(),
226+ new NetHttpTransport (), GsonFactory . getDefaultInstance(),
205227 " [[ENTER YOUR CLIENT ID]]" , " [[ENTER YOUR CLIENT SECRET]]" ,
206228 Collections . singleton(CalendarScopes . CALENDAR )). setDataStoreFactory(
207229 DATA_STORE_FACTORY ). setAccessType(" offline" ). build();
@@ -238,7 +260,7 @@ public class CalendarServletCallbackSample extends AbstractAuthorizationCodeCall
238260 @Override
239261 protected AuthorizationCodeFlow initializeFlow () throws IOException {
240262 return new GoogleAuthorizationCodeFlow .Builder (
241- new NetHttpTransport (), JacksonFactory . getDefaultInstance()
263+ new NetHttpTransport (), GsonFactory . getDefaultInstance()
242264 " [[ENTER YOUR CLIENT ID]]" , " [[ENTER YOUR CLIENT SECRET]]" ,
243265 Collections . singleton(CalendarScopes . CALENDAR )). setDataStoreFactory(
244266 DATA_STORE_FACTORY ). setAccessType(" offline" ). build();
@@ -342,7 +364,7 @@ For an additional sample, see
342364
343365### Service accounts
344366
345- [ GoogleCredential ] [ google-credential ] also supports [ service accounts] [ service-accounts ] .
367+ [ GoogleCredentials ] [ google-credentials ] also supports [ service accounts] [ service-accounts ] .
346368Unlike the credential in which a client application requests access to an
347369end-user's data, Service Accounts provide access to the client application's
348370own 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:
352374
353375``` java
354376HttpTransport httpTransport = GoogleNetHttpTransport . newTrustedTransport();
355- JsonFactory jsonFactory = JacksonFactory . getDefaultInstance();
377+ JsonFactory jsonFactory = GsonFactory . getDefaultInstance();
356378
357379// Build service account credential
358380GoogleCredentials googleCredentials = GoogleCredentials .
@@ -530,7 +552,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
530552}
531553```
532554
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
534556[google-oauth-client-instructions]: https://developers.google.com/api-client-library/java/google-oauth-java-client/oauth2
535557[oauth2]: https://developers.google.com/accounts/docs/OAuth2
536558[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) {
539561[console-help]: https://developer.google.com/console/help/console/
540562[identity-api]: https://cloud.google.com/appengine/docs/java/appidentity/?csw=1#Asserting_Identity_to_Google_APIs
541563[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
543564[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-
544565[data-store-factory]: https://googleapis.dev/java/google-http-client/latest/com/google/api/client/util/store/DataStoreFactory.html
545566[stored-credential]: https://googleapis.dev/java/google-oauth-client/latest/com/google/api/client/auth/oauth2/StoredCredential.html
0 commit comments