Skip to content

Commit 819e9c3

Browse files
Hiranya Jayathilakamattwhisenhunt
authored andcommitted
Implementing support for getAccessToken() method for AppIdentityCredentialWrapper
1 parent 3d9e969 commit 819e9c3

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

google-api-client-appengine/src/main/java/com/google/api/client/googleapis/extensions/appengine/auth/oauth2/AppIdentityCredential.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.api.client.googleapis.extensions.appengine.auth.oauth2;
1616

1717
import com.google.api.client.auth.oauth2.BearerToken;
18+
import com.google.api.client.auth.oauth2.TokenResponse;
1819
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
1920
import com.google.api.client.http.HttpExecuteInterceptor;
2021
import com.google.api.client.http.HttpRequest;
@@ -24,6 +25,7 @@
2425
import com.google.api.client.util.Beta;
2526
import com.google.api.client.util.Preconditions;
2627
import com.google.appengine.api.appidentity.AppIdentityService;
28+
import com.google.appengine.api.appidentity.AppIdentityService.GetAccessTokenResult;
2729
import com.google.appengine.api.appidentity.AppIdentityServiceFactory;
2830

2931
import java.io.IOException;
@@ -255,5 +257,17 @@ public GoogleCredential createScoped(Collection<String> scopes) {
255257
getTransport(),
256258
getJsonFactory());
257259
}
260+
261+
@Override
262+
protected TokenResponse executeRefreshToken() throws IOException {
263+
GetAccessTokenResult tokenResult = appIdentity.getAppIdentityService()
264+
.getAccessToken(appIdentity.getScopes());
265+
TokenResponse response = new TokenResponse();
266+
response.setAccessToken(tokenResult.getAccessToken());
267+
long expiresInSeconds =
268+
(tokenResult.getExpirationTime().getTime() - System.currentTimeMillis()) / 1000;
269+
response.setExpiresInSeconds(expiresInSeconds);
270+
return response;
271+
}
258272
}
259273
}

google-api-client-appengine/src/test/java/com/google/api/client/googleapis/extensions/appengine/auth/oauth2/AppIdentityCredentialTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,25 @@ public void testAppEngineCredentialWrapper() throws IOException {
105105
assertTrue(authHeader.contains(expectedAccessToken));
106106
}
107107

108+
public void testAppEngineCredentialWrapperGetAccessToken() {
109+
final String expectedAccessToken = "ExpectedAccessToken";
110+
final Collection<String> emptyScopes = Collections.emptyList();
111+
112+
HttpTransport transport = new MockHttpTransport();
113+
JsonFactory jsonFactory = new JacksonFactory();
114+
115+
MockAppIdentityService appIdentity = new MockAppIdentityService();
116+
appIdentity.setAccessTokenText(expectedAccessToken);
117+
118+
AppIdentityCredential.Builder builder = new AppIdentityCredential.Builder(emptyScopes);
119+
builder.setAppIdentityService(appIdentity);
120+
AppIdentityCredential appCredential = builder.build();
121+
122+
GoogleCredential wrapper = new
123+
AppIdentityCredential.AppEngineCredentialWrapper(appCredential, transport, jsonFactory);
124+
assertEquals(expectedAccessToken, wrapper.getAccessToken());
125+
}
126+
108127
public void testAppEngineCredentialWrapperNullTransportThrows() throws IOException {
109128
JsonFactory jsonFactory = new JacksonFactory();
110129
try {

0 commit comments

Comments
 (0)