Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directory API: Not Authorized to access this resource/api #4279

Open
somejavadev opened this issue Mar 13, 2020 · 1 comment
Open

Directory API: Not Authorized to access this resource/api #4279

somejavadev opened this issue Mar 13, 2020 · 1 comment
Labels
priority: p4 type: question Request for information or clarification. Not an issue.

Comments

@somejavadev
Copy link

Hi,

I am trying to list users within my gSuite domain from a gke cluster using a service account. I have done the following to test the service account with the google-api-services-admin-directory api:

Within my Google Cloud Account I have enabled the Admin SDK. I then created a domain-wide service account in the project as described here https://developers.google.com/admin-sdk/directory/v1/guides/delegation, then downloaded the JSON key file and gave it authorization to the following scopes in the Admin Console:

https://www.googleapis.com/auth/admin.directory.group.member.readonly 
https://www.googleapis.com/auth/admin.directory.group.readonly 
https://www.googleapis.com/auth/admin.directory.user.readonly 
https://www.googleapis.com/auth/admin.directory.user.security 

I then setup a very basic Java application to test listing groups:

Test.java

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.directory.Directory;
import com.google.api.services.directory.DirectoryScopes;
import com.google.api.services.directory.model.Groups;


import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.List;

public class Test {

    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    private static final List<String> SCOPES = new ArrayList<>();

    private static Credential getCredentials(NetHttpTransport netHttpTransport) throws IOException {
        SCOPES.add(DirectoryScopes.ADMIN_DIRECTORY_USER_READONLY);
        SCOPES.add(DirectoryScopes.ADMIN_DIRECTORY_GROUP_READONLY);
        SCOPES.add(DirectoryScopes.ADMIN_DIRECTORY_USER_SECURITY);
        GoogleCredential credential = GoogleCredential.getApplicationDefault(netHttpTransport, JSON_FACTORY).createScoped(SCOPES);
        return credential;
    }

    public static void main(String... args) throws IOException, GeneralSecurityException {
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        Directory directory = new Directory.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)).setApplicationName("test").build();
        Directory.Groups.List list = directory.groups().list();
        list.setDomain("mydomain.com");
        Groups users = list.execute();
        users.getGroups().forEach(g -> System.out.println(g.getName()));
    }
}

pom.xml

    <dependencies>
        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-admin-directory</artifactId>
            <version>directory_v1-rev20191003-1.30.8</version>
        </dependency>
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client</artifactId>
            <version>1.30.8</version>
        </dependency>
    </dependencies>

Before running the java file I set the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to my json service account file.

When running this no matter whether I am trying to access users or groups I keep getting the following error message from the API:

{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "Not Authorized to access this resource/api",
    "reason" : "forbidden"
  } ],
  "message" : "Not Authorized to access this resource/api"
}

Stack trace:

at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:150)
	at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
	at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:444)
	at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1108)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:542)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:475)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:592)

I believe this issue might be related to the issue mentioned in the google-api-nodejs-client project:
googleapis/google-api-nodejs-client#1884

Adding the scope https://www.googleapis.com/auth/admin.directory.user.security does not resolve the problem.

Regards

@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Mar 14, 2020
@melamber
Copy link

melamber commented Mar 14, 2020

The same for me. I created service account with full access. And added all necessary scopes.

https://www.googleapis.com/auth/admin.directory.user
https://www.googleapis.com/auth/admin.directory.user.security
and etc.

But can't get any user. Get this error every time Directory API: Not Authorized to access this resource/api

@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Mar 18, 2020
@chingor13 chingor13 added the type: question Request for information or clarification. Not an issue. label Mar 24, 2020
@yoshi-automation yoshi-automation removed triage me I really want to be triaged. 🚨 This issue needs some love. labels Mar 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p4 type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

5 participants