From 043adf86fe3fa9d39a85e372046f93e6ccac98f0 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Tue, 3 Dec 2019 15:23:45 -0800 Subject: [PATCH 1/4] chore(docs): update GoogleCredential samples to use google-auth-library --- docs/oauth-2.0.md | 72 +++++++++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/docs/oauth-2.0.md b/docs/oauth-2.0.md index df8a044ee..31ccbda70 100644 --- a/docs/oauth-2.0.md +++ b/docs/oauth-2.0.md @@ -38,21 +38,32 @@ that runs in browser. For instructions on setting up your credentials properly, see the [API Console Help][console-help]. -## Credential +## Credentials -### GoogleCredential +### GoogleCredentials -[`GoogleCredential`][google-credential] is a thread-safe helper class for OAuth +[`GoogleCredentials`][google-credentials] is a thread-safe helper class for OAuth 2.0 for accessing protected resources using an access token. For example, if you already have an access token, you can make a request in the following way: ```java -GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken); -Plus plus = new Plus.builder(new NetHttpTransport(), - JacksonFactory.getDefaultInstance(), - credential) - .setApplicationName("Google-PlusSample/1.0") - .build(); +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.books.Books; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.AccessToken; +import com.google.auth.oauth2.GoogleCredentials; + +GoogleCredentials credentials = + GoogleCredentials.newBuilder().setAccessToken(new AccessToken("token", null)).build(); + +Books books = + new Books.Builder( + new NetHttpTransport(), + JacksonFactory.getDefaultInstance(), + new HttpCredentialsAdapter(credentials)) + .setApplicationName("BooksExample/1.0") + .build(); ``` ### Google App Engine identity @@ -67,24 +78,38 @@ Use [`AppIdentityCredential`][app-identity-credential] (from App Engine takes care of all of the details. You only specify the OAuth 2.0 scope you need. -Example code taken from [urlshortener-robots-appengine-sample][urlshortener-sample]: - ```java -static Urlshortener newUrlshortener() { - AppIdentityCredential credential = - new AppIdentityCredential( - Collections.singletonList(UrlshortenerScopes.URLSHORTENER)); - return new Urlshortener.Builder(new UrlFetchTransport(), - JacksonFactory.getDefaultInstance(), - credential) - .build(); -} +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.books.Books; +import com.google.appengine.api.appidentity.AppIdentityService; +import com.google.appengine.api.appidentity.AppIdentityServiceFactory; +import com.google.auth.appengine.AppEngineCredentials; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; +import java.util.Arrays; + +AppIdentityService appIdentityService = AppIdentityServiceFactory.getAppIdentityService(); + +GoogleCredentials credentials = + AppEngineCredentials.newBuilder() + .setScopes(Arrays.asList("scope1", "scope2", "scope3")) + .setAppIdentityService(appIdentityService) + .build(); + +Books books = + new Books.Builder( + new NetHttpTransport(), + JacksonFactory.getDefaultInstance(), + new HttpCredentialsAdapter(credentials)) + .setApplicationName("BooksExample/1.0") + .build(); ``` ## Data store An access token typically has an expiration date of 1 hour, after which you will -get an error if you try to use it. [GoogleCredential][google-credential] takes +get an error if you try to use it. [GoogleCredentials][google-credentials] takes care of automatically "refreshing" the token, which simply means getting a new access token. This is done by means of a long-lived refresh token, which is typically received along with the access token if you use the @@ -339,7 +364,7 @@ For an additional sample, see ### Service accounts -[GoogleCredential][google-credential] also supports [service accounts][service-accounts]. +[GoogleCredentials][google-credentials] also supports [service accounts][service-accounts]. Unlike the credential in which a client application requests access to an end-user's data, Service Accounts provide access to the client application's own data. Your client application signs the request for an access token using @@ -527,7 +552,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { } ``` -[google-credential]: https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/auth/oauth2/GoogleCredential.html +[google-credentials]: https://googleapis.dev/java/google-auth-library/latest/index.html?com/google/auth/oauth2/GoogleCredentials.html [google-oauth-client-instructions]: https://developers.google.com/api-client-library/java/google-oauth-java-client/oauth2 [oauth2]: https://developers.google.com/accounts/docs/OAuth2 [javadoc-oauth2]: https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/auth/oauth2/package-frame.html @@ -536,7 +561,6 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { [console-help]: https://developer.google.com/console/help/console/ [identity-api]: https://cloud.google.com/appengine/docs/java/appidentity/?csw=1#Asserting_Identity_to_Google_APIs [app-identity-credential]: https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/extensions/appengine/auth/oauth2/AppIdentityCredential.html -[urlshortener-sample]: https://github.com/google/google-api-java-client-samples/tree/master/urlshortener-robots-appengine-sample [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- [data-store-factory]: https://googleapis.dev/java/google-http-client/latest/com/google/api/client/util/store/DataStoreFactory.html [stored-credential]: https://googleapis.dev/java/google-oauth-client/latest/com/google/api/client/auth/oauth2/StoredCredential.html From f6bc402f9aa1612a984e03c258d41c8418cfb2ec Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Thu, 14 Jan 2021 14:56:13 -0800 Subject: [PATCH 2/4] use gson --- docs/oauth-2.0.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/oauth-2.0.md b/docs/oauth-2.0.md index c8d9b4013..44bab2d5e 100644 --- a/docs/oauth-2.0.md +++ b/docs/oauth-2.0.md @@ -48,7 +48,7 @@ already have an access token, you can make a request in the following way: ```java import com.google.api.client.http.javanet.NetHttpTransport; -import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.books.Books; import com.google.auth.http.HttpCredentialsAdapter; import com.google.auth.oauth2.AccessToken; @@ -60,7 +60,7 @@ GoogleCredentials credentials = Books books = new Books.Builder( new NetHttpTransport(), - JacksonFactory.getDefaultInstance(), + GsonFactory.getDefaultInstance(), new HttpCredentialsAdapter(credentials)) .setApplicationName("BooksExample/1.0") .build(); @@ -80,7 +80,7 @@ scope you need. ```java import com.google.api.client.http.javanet.NetHttpTransport; -import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.books.Books; import com.google.appengine.api.appidentity.AppIdentityService; import com.google.appengine.api.appidentity.AppIdentityServiceFactory; @@ -100,7 +100,7 @@ GoogleCredentials credentials = Books books = new Books.Builder( new NetHttpTransport(), - JacksonFactory.getDefaultInstance(), + GsonFactory.getDefaultInstance(), new HttpCredentialsAdapter(credentials)) .setApplicationName("BooksExample/1.0") .build(); @@ -223,7 +223,7 @@ public class CalendarServletSample extends AbstractAuthorizationCodeServlet { @Override protected AuthorizationCodeFlow initializeFlow() throws IOException { return new GoogleAuthorizationCodeFlow.Builder( - new NetHttpTransport(), JacksonFactory.getDefaultInstance(), + new NetHttpTransport(), GsonFactory.getDefaultInstance(), "[[ENTER YOUR CLIENT ID]]", "[[ENTER YOUR CLIENT SECRET]]", Collections.singleton(CalendarScopes.CALENDAR)).setDataStoreFactory( DATA_STORE_FACTORY).setAccessType("offline").build(); @@ -260,7 +260,7 @@ public class CalendarServletCallbackSample extends AbstractAuthorizationCodeCall @Override protected AuthorizationCodeFlow initializeFlow() throws IOException { return new GoogleAuthorizationCodeFlow.Builder( - new NetHttpTransport(), JacksonFactory.getDefaultInstance() + new NetHttpTransport(), GsonFactory.getDefaultInstance() "[[ENTER YOUR CLIENT ID]]", "[[ENTER YOUR CLIENT SECRET]]", Collections.singleton(CalendarScopes.CALENDAR)).setDataStoreFactory( DATA_STORE_FACTORY).setAccessType("offline").build(); @@ -374,7 +374,7 @@ For example, you can make a request in the following way: ```java HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); -JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); +JsonFactory jsonFactory = GsonFactory.getDefaultInstance(); //Build service account credential GoogleCredentials googleCredentials = GoogleCredentials. From a95f4b957a32eb05413ad0e09814bb2866277ab2 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Thu, 14 Jan 2021 14:58:27 -0800 Subject: [PATCH 3/4] use trusted transport: --- docs/oauth-2.0.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/oauth-2.0.md b/docs/oauth-2.0.md index 44bab2d5e..a45d4723b 100644 --- a/docs/oauth-2.0.md +++ b/docs/oauth-2.0.md @@ -47,7 +47,7 @@ For instructions on setting up your credentials properly, see the already have an access token, you can make a request in the following way: ```java -import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.books.Books; import com.google.auth.http.HttpCredentialsAdapter; @@ -59,7 +59,7 @@ GoogleCredentials credentials = Books books = new Books.Builder( - new NetHttpTransport(), + GoogleNetHttpTransport.newTrustedTransport(), GsonFactory.getDefaultInstance(), new HttpCredentialsAdapter(credentials)) .setApplicationName("BooksExample/1.0") @@ -79,7 +79,7 @@ App Engine takes care of all of the details. You only specify the OAuth 2.0 scope you need. ```java -import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.books.Books; import com.google.appengine.api.appidentity.AppIdentityService; @@ -99,7 +99,7 @@ GoogleCredentials credentials = Books books = new Books.Builder( - new NetHttpTransport(), + GoogleNetHttpTransport.newTrustedTransport(), GsonFactory.getDefaultInstance(), new HttpCredentialsAdapter(credentials)) .setApplicationName("BooksExample/1.0") From 2d54d5b11c6426f84fd5c088d8e5916661d841e7 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Thu, 14 Jan 2021 14:59:12 -0800 Subject: [PATCH 4/4] fix mismerge --- docs/oauth-2.0.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/docs/oauth-2.0.md b/docs/oauth-2.0.md index a45d4723b..56a72b165 100644 --- a/docs/oauth-2.0.md +++ b/docs/oauth-2.0.md @@ -552,11 +552,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { } ``` -<<<<<<< HEAD [google-credentials]: https://googleapis.dev/java/google-auth-library/latest/index.html?com/google/auth/oauth2/GoogleCredentials.html -======= -[google-credential]: https://googleapis.dev/java/google-auth-library/latest/com/google/auth/oauth2/GoogleCredentials.html ->>>>>>> master [google-oauth-client-instructions]: https://developers.google.com/api-client-library/java/google-oauth-java-client/oauth2 [oauth2]: https://developers.google.com/accounts/docs/OAuth2 [javadoc-oauth2]: https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/auth/oauth2/package-frame.html