Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(docs): update GoogleCredential samples to use google-auth-library #1440

Merged
merged 5 commits into from Feb 12, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 48 additions & 24 deletions docs/oauth-2.0.md
Expand Up @@ -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
Expand All @@ -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(),
chingor13 marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down