Skip to content

Service account: Cannot set user to impersonate using GoogleCredential fromStream #1007

Closed

Description

@WilsonGiese

I'm trying to use a service account to automatically manage/create groups using the Directory API. When I try to use a service account with the credentials in the form of JSON and try to use the GoogleCredential.fromStream(...) call, there does not seem to be a way to set the user, and at least for the Admin API I cannot get calls to work without impersonating a user as I always get Forbidden (403) responses.

Using the GoogleCredential Builder seems to work fine with the PKCS12 formatted key as long as I set the user, but your docs and console seem to recommend to use the JSON formatted credential file.

Have I missed something, or is this functionality not available without using the builder?

Activity

  1. ejona86 commented on Apr 14, 2016

    @ejona86
    Contributor

    as long as I set the user

    How are you going about setting the user in the PKCS12 case?

  2. abhishek-choudhury commented on Jun 20, 2016

    @abhishek-choudhury

    Hey Wilson,

    Did you get a response on this issue?
    Am facing the exact same thing. I wonder how one can set a service account ID whilst building GoogleCredential fromStream.

  3. WilsonGiese commented on Jun 20, 2016

    @WilsonGiese
    Author

    @abhishek-choudhury No, I never heard anything. I resorted to using the builder with a PKCS12 file.

  4. abhishek-choudhury commented on Jun 20, 2016

    @abhishek-choudhury

    @WilsonGiese Thanks for your response Wilson!
    I'll go back to P12 too in that case.

  5. netp-dany commented on Dec 1, 2016

    @netp-dany

    Hi,

    There is a work around, please have a look at the code below.
    In case you really need to use json.

    public GoogleCredential getCredentials(HttpTransport httpTransport, JsonFactory jsonFactory) throws GeneralSecurityException, IOException {
        List<String> scopes = new ArrayList<>();
        scopes.add(DirectoryScopes.ADMIN_DIRECTORY_USER);
        scopes.add(DirectoryScopes.ADMIN_DIRECTORY_USER_READONLY);
    
        InputStream credentialsJSON = ServiceAccountCredentials.class.getClassLoader()
                .getResourceAsStream("service_account.json");
    
        GoogleCredential gcFromJson = GoogleCredential.fromStream(credentialsJSON, httpTransport, jsonFactory).createScoped(scopes);
    
        return new GoogleCredential.Builder()
                .setTransport(gcFromJson.getTransport())
                .setJsonFactory(gcFromJson.getJsonFactory())
                .setServiceAccountId(gcFromJson.getServiceAccountId())
                .setServiceAccountUser("user@example.com")
                .setServiceAccountPrivateKey(gcFromJson.getServiceAccountPrivateKey())
                .setServiceAccountScopes(gcFromJson.getServiceAccountScopes())
                .build();
    }
  6. sanbornick commented on Mar 1, 2017

    @sanbornick

    JSON all the things! The console will issue JSON by default and make you dig for P12's. Lets go future! Except here. Don't look here.

    This issue has only been open for a year I guess...

  7. nddipiazza commented on Jul 27, 2017

    @nddipiazza

    @netp-dany awesome! i've been looking for hours for an example. aka you're a f*king genius

  8. nbali commented on Jan 6, 2018

    @nbali

    Will one of the PRs aimed at solving this will be merged? #1036 and/or #1079?

  9. added
    priority: p1
    on Jan 8, 2018
  10. added a commit that references this issue on May 25, 2018
  11. added
    type: feature request
    🚨
    and removed
    priority: p1
    on Jun 7, 2018
  12. removed
    🚨
    on Jun 25, 2018
  13. chingor13 commented on Sep 12, 2018

    @chingor13
    Collaborator

    Fixed in #1079

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: feature request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions