16
16
17
17
package com .example .appengine .firetactoe ;
18
18
19
- import com .google .api .client .auth .oauth2 .Credential ;
20
19
import com .google .api .client .extensions .appengine .http .UrlFetchTransport ;
21
- import com .google .api .client .googleapis .auth .oauth2 .GoogleCredential ;
22
20
import com .google .api .client .http .ByteArrayContent ;
23
21
import com .google .api .client .http .GenericUrl ;
24
22
import com .google .api .client .http .HttpRequestFactory ;
25
23
import com .google .api .client .http .HttpResponse ;
26
24
import com .google .api .client .http .HttpTransport ;
27
25
import com .google .appengine .api .appidentity .AppIdentityService ;
28
26
import com .google .appengine .api .appidentity .AppIdentityServiceFactory ;
27
+ import com .google .auth .http .HttpCredentialsAdapter ;
28
+ import com .google .auth .oauth2 .GoogleCredentials ;
29
29
import com .google .common .io .BaseEncoding ;
30
30
import com .google .common .io .CharStreams ;
31
31
import com .google .gson .Gson ;
@@ -56,7 +56,7 @@ public class FirebaseChannel {
56
56
"https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit" ;
57
57
58
58
private String firebaseDbUrl ;
59
- private GoogleCredential credential ;
59
+ private GoogleCredentials credential ;
60
60
// Keep this a package-private member variable, so that it can be mocked for unit tests
61
61
HttpTransport httpTransport ;
62
62
@@ -91,7 +91,7 @@ private FirebaseChannel() {
91
91
CharStreams .toString (new InputStreamReader (firebaseConfigStream , StandardCharsets .UTF_8 ));
92
92
firebaseDbUrl = parseFirebaseUrl (firebaseSnippet );
93
93
94
- credential = GoogleCredential .getApplicationDefault ().createScoped (FIREBASE_SCOPES );
94
+ credential = GoogleCredentials .getApplicationDefault ().createScoped (FIREBASE_SCOPES );
95
95
httpTransport = UrlFetchTransport .getDefaultInstance ();
96
96
} catch (IOException e ) {
97
97
throw new RuntimeException (e );
@@ -117,13 +117,15 @@ private static String parseFirebaseUrl(String firebaseSnippet) {
117
117
118
118
/**
119
119
* sendFirebaseMessage.
120
+ *
120
121
* @param channelKey .
121
122
* @param game .
122
123
* @throws IOException .
123
124
*/
124
125
public void sendFirebaseMessage (String channelKey , Game game ) throws IOException {
125
126
// Make requests auth'ed using Application Default Credentials
126
- HttpRequestFactory requestFactory = httpTransport .createRequestFactory (credential );
127
+ HttpRequestFactory requestFactory =
128
+ httpTransport .createRequestFactory (new HttpCredentialsAdapter (credential ));
127
129
GenericUrl url =
128
130
new GenericUrl (String .format ("%s/channels/%s.json" , firebaseDbUrl , channelKey ));
129
131
HttpResponse response = null ;
@@ -152,9 +154,7 @@ url, new ByteArrayContent("application/json", gameJson.getBytes()))
152
154
}
153
155
}
154
156
155
- /**
156
- * Create a secure JWT token for the given userId.
157
- */
157
+ /** Create a secure JWT token for the given userId. */
158
158
public String createFirebaseToken (Game game , String userId ) {
159
159
final AppIdentityService appIdentity = AppIdentityServiceFactory .getAppIdentityService ();
160
160
final BaseEncoding base64 = BaseEncoding .base64 ();
@@ -186,15 +186,18 @@ public String createFirebaseToken(Game game, String userId) {
186
186
187
187
/**
188
188
* firebasePut.
189
+ *
189
190
* @param path .
190
191
* @param object .
191
192
* @return .
192
193
* @throws IOException .
193
194
*/
194
195
public HttpResponse firebasePut (String path , Object object ) throws IOException {
195
196
// Make requests auth'ed using Application Default Credentials
196
- Credential credential = GoogleCredential .getApplicationDefault ().createScoped (FIREBASE_SCOPES );
197
- HttpRequestFactory requestFactory = httpTransport .createRequestFactory (credential );
197
+ GoogleCredentials credential =
198
+ GoogleCredentials .getApplicationDefault ().createScoped (FIREBASE_SCOPES );
199
+ HttpRequestFactory requestFactory =
200
+ httpTransport .createRequestFactory (new HttpCredentialsAdapter (credential ));
198
201
199
202
String json = new Gson ().toJson (object );
200
203
GenericUrl url = new GenericUrl (path );
@@ -206,15 +209,18 @@ public HttpResponse firebasePut(String path, Object object) throws IOException {
206
209
207
210
/**
208
211
* firebasePatch.
212
+ *
209
213
* @param path .
210
214
* @param object .
211
215
* @return .
212
216
* @throws IOException .
213
217
*/
214
218
public HttpResponse firebasePatch (String path , Object object ) throws IOException {
215
219
// Make requests auth'ed using Application Default Credentials
216
- Credential credential = GoogleCredential .getApplicationDefault ().createScoped (FIREBASE_SCOPES );
217
- HttpRequestFactory requestFactory = httpTransport .createRequestFactory (credential );
220
+ GoogleCredentials credential =
221
+ GoogleCredentials .getApplicationDefault ().createScoped (FIREBASE_SCOPES );
222
+ HttpRequestFactory requestFactory =
223
+ httpTransport .createRequestFactory (new HttpCredentialsAdapter (credential ));
218
224
219
225
String json = new Gson ().toJson (object );
220
226
GenericUrl url = new GenericUrl (path );
@@ -226,15 +232,18 @@ public HttpResponse firebasePatch(String path, Object object) throws IOException
226
232
227
233
/**
228
234
* firebasePost.
235
+ *
229
236
* @param path .
230
237
* @param object .
231
238
* @return .
232
239
* @throws IOException .
233
240
*/
234
241
public HttpResponse firebasePost (String path , Object object ) throws IOException {
235
242
// Make requests auth'ed using Application Default Credentials
236
- Credential credential = GoogleCredential .getApplicationDefault ().createScoped (FIREBASE_SCOPES );
237
- HttpRequestFactory requestFactory = httpTransport .createRequestFactory (credential );
243
+ GoogleCredentials credential =
244
+ GoogleCredentials .getApplicationDefault ().createScoped (FIREBASE_SCOPES );
245
+ HttpRequestFactory requestFactory =
246
+ httpTransport .createRequestFactory (new HttpCredentialsAdapter (credential ));
238
247
239
248
String json = new Gson ().toJson (object );
240
249
GenericUrl url = new GenericUrl (path );
@@ -246,14 +255,17 @@ public HttpResponse firebasePost(String path, Object object) throws IOException
246
255
247
256
/**
248
257
* firebaseGet.
258
+ *
249
259
* @param path .
250
260
* @return .
251
261
* @throws IOException .
252
262
*/
253
263
public HttpResponse firebaseGet (String path ) throws IOException {
254
264
// Make requests auth'ed using Application Default Credentials
255
- Credential credential = GoogleCredential .getApplicationDefault ().createScoped (FIREBASE_SCOPES );
256
- HttpRequestFactory requestFactory = httpTransport .createRequestFactory (credential );
265
+ GoogleCredentials credential =
266
+ GoogleCredentials .getApplicationDefault ().createScoped (FIREBASE_SCOPES );
267
+ HttpRequestFactory requestFactory =
268
+ httpTransport .createRequestFactory (new HttpCredentialsAdapter (credential ));
257
269
258
270
GenericUrl url = new GenericUrl (path );
259
271
@@ -262,14 +274,17 @@ public HttpResponse firebaseGet(String path) throws IOException {
262
274
263
275
/**
264
276
* firebaseDelete.
277
+ *
265
278
* @param path .
266
279
* @return .
267
280
* @throws IOException .
268
281
*/
269
282
public HttpResponse firebaseDelete (String path ) throws IOException {
270
283
// Make requests auth'ed using Application Default Credentials
271
- Credential credential = GoogleCredential .getApplicationDefault ().createScoped (FIREBASE_SCOPES );
272
- HttpRequestFactory requestFactory = httpTransport .createRequestFactory (credential );
284
+ GoogleCredentials credential =
285
+ GoogleCredentials .getApplicationDefault ().createScoped (FIREBASE_SCOPES );
286
+ HttpRequestFactory requestFactory =
287
+ httpTransport .createRequestFactory (new HttpCredentialsAdapter (credential ));
273
288
274
289
GenericUrl url = new GenericUrl (path );
275
290
0 commit comments