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

Access granted : false #354

Open
jonathanVeyssiere opened this issue Mar 22, 2022 · 1 comment
Open

Access granted : false #354

jonathanVeyssiere opened this issue Mar 22, 2022 · 1 comment

Comments

@jonathanVeyssiere
Copy link

jonathanVeyssiere commented Mar 22, 2022

Hello,

I have an issue with the usage of this library aiming at using a service account with a wide delegation on corporate email. I'd like to generate a token usable to proceed to an api call made in the name of one of my user using AppScrit (impersonate).

I proceed to the previous steps below :

  1. Create a service account
  2. Authorize the client_id on Admin console for the dedicated api scope on wide delegation
  3. Create and download a JSON key attached to that service account
  4. Enable the dedicated API on the GCP project (Google Analytics API)

It is only working when the setIssuer(...............) and .setSubject(...............) are with the json.client_email.

Do you know how to handle such requests with App script ? How can I make a such a call ?

Sample of the test code :

function getUserToken() {
var json = {
"type": "service_account",
"project_id": "....................",
"private_key_id": "....................",
"private_key": "....................",
"client_email": "....................",
"client_id": "....................",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "....................",
"client_x509_cert_url": "...................."
}
var service = getOAuthService(json);
service.reset();
Logger.log(service.getAccessToken());
if (service.hasAccess()) {
Logger.log(service.getAccessToken());
}
}

function getOAuthService(json) {
return OAuth2.createService("Service Account")
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
.setPrivateKey(json.private_key)
.setIssuer(json.client_email)
.setSubject("....................")
.setPropertyStore(PropertiesService.getScriptProperties())
.setParam('access_type', 'offline')
.setScope('https://www.googleapis.com/auth/analytics.readonly');
}

function reset(json) {
var service = getOAuthService(json);
service.reset();
}

@erickoledadevrel
Copy link
Contributor

For step #2, did you follow the directions here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants