Skip to content

Commit 1cd2a35

Browse files
authored
chore(docs): Update code snippets to use newer version of GoogleCredentials (#1655)
Fixes #1366 ☕️
1 parent 185476a commit 1cd2a35

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

docs/oauth-2.0.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,15 @@ For instructions on setting up your credentials properly, see the
4747
already have an access token, you can make a request in the following way:
4848

4949
```java
50-
GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);
51-
Plus plus = new Plus.builder(new NetHttpTransport(),
52-
JacksonFactory.getDefaultInstance(),
53-
credential)
54-
.setApplicationName("Google-PlusSample/1.0")
55-
.build();
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")
58+
.build();
5659
```
5760

5861
### Google App Engine identity
@@ -345,20 +348,20 @@ end-user's data, Service Accounts provide access to the client application's
345348
own data. Your client application signs the request for an access token using
346349
a private key downloaded from the [Google API Console][console].
347350

348-
Example code taken from [plus-serviceaccount-cmdline-sample][plus-sample]:
351+
For example, you can make a request in the following way:
349352

350353
```java
351354
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
352355
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
353-
...
354-
// Build service account credential.
355-
356-
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("MyProject-1234.json"))
357-
.createScoped(Collections.singleton(PlusScopes.PLUS_ME));
358-
// Set up global Plus instance.
359-
plus = new Plus.Builder(httpTransport, jsonFactory, credential)
360-
.setApplicationName(APPLICATION_NAME).build();
361-
...
356+
357+
//Build service account credential
358+
GoogleCredentials googleCredentials = GoogleCredentials.
359+
fromStream(new FileInputStream("/path/to/file"));
360+
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(googleCredentials);
361+
362+
Storage storage = new Storage.Builder(httpTransport, jsonFactory, requestInitializer)
363+
.setApplicationName("MyProject-1234")
364+
.build();
362365
```
363366

364367
For an additional sample, see [storage-serviceaccount-cmdline-sample][storage-sample].

0 commit comments

Comments
 (0)