@@ -186,6 +186,7 @@ public void testFromStreamServiceAccount() throws IOException {
186186 "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com" ;
187187 final String serviceAccountEmail =
188188 "36680232662-vrd7ji19qgchd0ah2csanun6bnr@developer.gserviceaccount.com" ;
189+ final String projectId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr" ;
189190
190191 MockTokenServerTransport transport = new MockTokenServerTransport ();
191192 transport .addServiceAccount (serviceAccountEmail , accessToken );
@@ -197,6 +198,7 @@ public void testFromStreamServiceAccount() throws IOException {
197198 serviceAccountContents .put ("client_email" , serviceAccountEmail );
198199 serviceAccountContents .put ("private_key" , SA_KEY_TEXT );
199200 serviceAccountContents .put ("private_key_id" , SA_KEY_ID );
201+ serviceAccountContents .put ("project_id" , projectId );
200202 serviceAccountContents .put ("type" , GoogleCredential .SERVICE_ACCOUNT_FILE_TYPE );
201203 String json = serviceAccountContents .toPrettyString ();
202204 InputStream serviceAccountStream = new ByteArrayInputStream (json .getBytes ());
@@ -216,6 +218,7 @@ public void testFromStreamServiceAccountAlternateTokenUri() throws IOException {
216218 "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com" ;
217219 final String serviceAccountEmail =
218220 "36680232662-vrd7ji19qgchd0ah2csanun6bnr@developer.gserviceaccount.com" ;
221+ final String projectId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr" ;
219222
220223 final String tokenServerUrl = "http://another.auth.com/token" ;
221224 MockTokenServerTransport transport = new MockTokenServerTransport (tokenServerUrl );
@@ -228,6 +231,7 @@ public void testFromStreamServiceAccountAlternateTokenUri() throws IOException {
228231 serviceAccountContents .put ("client_email" , serviceAccountEmail );
229232 serviceAccountContents .put ("private_key" , SA_KEY_TEXT );
230233 serviceAccountContents .put ("private_key_id" , SA_KEY_ID );
234+ serviceAccountContents .put ("project_id" , projectId );
231235 serviceAccountContents .put ("type" , GoogleCredential .SERVICE_ACCOUNT_FILE_TYPE );
232236 serviceAccountContents .put ("token_uri" , tokenServerUrl );
233237 String json = serviceAccountContents .toPrettyString ();
@@ -247,6 +251,7 @@ public void testFromStreamServiceAccountAlternateTokenUri() throws IOException {
247251 public void testFromStreamServiceAccountMissingClientIdThrows () throws IOException {
248252 final String serviceAccountEmail =
249253 "36680232662-vrd7ji19qgchd0ah2csanun6bnr@developer.gserviceaccount.com" ;
254+ final String projectId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr" ;
250255
251256 MockHttpTransport transport = new MockTokenServerTransport ();
252257
@@ -256,6 +261,7 @@ public void testFromStreamServiceAccountMissingClientIdThrows() throws IOExcepti
256261 serviceAccountContents .put ("client_email" , serviceAccountEmail );
257262 serviceAccountContents .put ("private_key" , SA_KEY_TEXT );
258263 serviceAccountContents .put ("private_key_id" , SA_KEY_ID );
264+ serviceAccountContents .put ("project_id" , projectId );
259265 serviceAccountContents .put ("type" , GoogleCredential .SERVICE_ACCOUNT_FILE_TYPE );
260266 String json = serviceAccountContents .toPrettyString ();
261267 InputStream serviceAccountStream = new ByteArrayInputStream (json .getBytes ());
@@ -271,6 +277,7 @@ public void testFromStreamServiceAccountMissingClientIdThrows() throws IOExcepti
271277 public void testFromStreamServiceAccountMissingClientEmailThrows () throws IOException {
272278 final String serviceAccountId =
273279 "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com" ;
280+ final String projectId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr" ;
274281
275282 MockHttpTransport transport = new MockTokenServerTransport ();
276283
@@ -280,6 +287,7 @@ public void testFromStreamServiceAccountMissingClientEmailThrows() throws IOExce
280287 serviceAccountContents .put ("client_id" , serviceAccountId );
281288 serviceAccountContents .put ("private_key" , SA_KEY_TEXT );
282289 serviceAccountContents .put ("private_key_id" , SA_KEY_ID );
290+ serviceAccountContents .put ("project_id" , projectId );
283291 serviceAccountContents .put ("type" , GoogleCredential .SERVICE_ACCOUNT_FILE_TYPE );
284292 String json = serviceAccountContents .toPrettyString ();
285293 InputStream serviceAccountStream = new ByteArrayInputStream (json .getBytes ());
@@ -297,6 +305,7 @@ public void testFromStreamServiceAccountMissingPrivateKeyThrows() throws IOExcep
297305 "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com" ;
298306 final String serviceAccountEmail =
299307 "36680232662-vrd7ji19qgchd0ah2csanun6bnr@developer.gserviceaccount.com" ;
308+ final String projectId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr" ;
300309
301310 MockHttpTransport transport = new MockTokenServerTransport ();
302311
@@ -306,6 +315,7 @@ public void testFromStreamServiceAccountMissingPrivateKeyThrows() throws IOExcep
306315 serviceAccountContents .put ("client_id" , serviceAccountId );
307316 serviceAccountContents .put ("client_email" , serviceAccountEmail );
308317 serviceAccountContents .put ("private_key_id" , SA_KEY_ID );
318+ serviceAccountContents .put ("project_id" , projectId );
309319 serviceAccountContents .put ("type" , GoogleCredential .SERVICE_ACCOUNT_FILE_TYPE );
310320 String json = serviceAccountContents .toPrettyString ();
311321 InputStream serviceAccountStream = new ByteArrayInputStream (json .getBytes ());
@@ -323,6 +333,7 @@ public void testFromStreamServiceAccountMissingPrivateKeyIdThrows() throws IOExc
323333 "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com" ;
324334 final String serviceAccountEmail =
325335 "36680232662-vrd7ji19qgchd0ah2csanun6bnr@developer.gserviceaccount.com" ;
336+ final String projectId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr" ;
326337
327338 MockHttpTransport transport = new MockTokenServerTransport ();
328339
@@ -332,6 +343,7 @@ public void testFromStreamServiceAccountMissingPrivateKeyIdThrows() throws IOExc
332343 serviceAccountContents .put ("client_id" , serviceAccountId );
333344 serviceAccountContents .put ("client_email" , serviceAccountEmail );
334345 serviceAccountContents .put ("private_key" , SA_KEY_TEXT );
346+ serviceAccountContents .put ("project_id" , projectId );
335347 serviceAccountContents .put ("type" , GoogleCredential .SERVICE_ACCOUNT_FILE_TYPE );
336348 String json = serviceAccountContents .toPrettyString ();
337349 InputStream serviceAccountStream = new ByteArrayInputStream (json .getBytes ());
@@ -344,6 +356,33 @@ public void testFromStreamServiceAccountMissingPrivateKeyIdThrows() throws IOExc
344356 }
345357 }
346358
359+ public void testFromStreamServiceAccountMissingProjectIdThrows () throws IOException {
360+ final String serviceAccountId =
361+ "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com" ;
362+ final String serviceAccountEmail =
363+ "36680232662-vrd7ji19qgchd0ah2csanun6bnr@developer.gserviceaccount.com" ;
364+
365+ MockHttpTransport transport = new MockTokenServerTransport ();
366+
367+ // Write out user file
368+ GenericJson serviceAccountContents = new GenericJson ();
369+ serviceAccountContents .setFactory (JSON_FACTORY );
370+ serviceAccountContents .put ("client_id" , serviceAccountId );
371+ serviceAccountContents .put ("client_email" , serviceAccountEmail );
372+ serviceAccountContents .put ("private_key" , SA_KEY_TEXT );
373+ serviceAccountContents .put ("private_key_id" , SA_KEY_ID );
374+ serviceAccountContents .put ("type" , GoogleCredential .SERVICE_ACCOUNT_FILE_TYPE );
375+ String json = serviceAccountContents .toPrettyString ();
376+ InputStream serviceAccountStream = new ByteArrayInputStream (json .getBytes ());
377+
378+ try {
379+ GoogleCredential .fromStream (serviceAccountStream , transport , JSON_FACTORY );
380+ fail ();
381+ } catch (IOException expected ) {
382+ assertTrue (expected .getMessage ().contains ("project_id" ));
383+ }
384+ }
385+
347386 public void testFromStreamUser () throws IOException {
348387 final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
349388 final String clientSecret = "jakuaL9YyieakhECKL2SwZcu" ;
0 commit comments