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

How to pass data for access token as contentType: application/x-www-form-urlencoded #388

Open
dking12 opened this issue Jul 25, 2022 · 3 comments
Assignees

Comments

@dking12
Copy link

dking12 commented Jul 25, 2022

I couldn't find any examples that show how to utilize the library for services that require the access token request to provide data in the contentType of application/x-www-form-urlencoded. One example is using the Box API using the client credentials grant authorization method explained here: https://developer.box.com/guides/authentication/client-credentials/.

I've created a function which can perform this action but I'd prefer to use any built-in functions of this library.

@erickoledadevrel
Copy link
Contributor

I believe the library uses application/x-www-form-urlencoded by default for all token requests. The library can handle the token being returns as either JSON or form encoded, but the requests are always form encoded.

@mc-ct
Copy link

mc-ct commented Sep 27, 2022

@dking12 Have you figured out how to use this library with the Box API?

I have created a Box app with Authentication Method OAuth 2.0 with Client Credentials Grant (Server Authentication).

As per the documentation, the Content-Type should be set to application/x-www-form-urlencoded in the request headers, and the following values should be sent in the request body:

client_id=[CLIENT_ID]
client_secret=[CLIENT_SECRET]
grant_type=client_credentials
box_subject_type=enterprise
box_subject_id=[ENTERPRISE_ID]

When running this:

function getBoxService() {
  const service = OAuth2.createService('Box')
    .setTokenUrl('https://api.box.com/oauth2/token')
    .setGrantType('client_credentials')
    .setTokenHeaders({
      'Content-Type': 'application/x-www-form-urlencoded'
    })
    .setParam('client_id', BOX_API_CLIENT_ID)
    .setParam('client_secret', BOX_API_CLIENT_SECRET)
    .setParam('grant_type', 'client_credentials')
    .setParam('box_subject_type', 'enterprise')
    .setParam('box_subject_id', BOX_ENTERPRISE_ID)
    .setPropertyStore(PropertiesService.getUserProperties());

  if (!service.hasAccess()) Logger.log(service.getLastError());
}

I get:

Error: Error retrieving token: invalid_grant, Grant credentials are invalid

I have confirmed that the correct Client ID, Client Secret, and Enterprise ID are being provided, and that the App Access Level to App + Enterprise Access (which the documentation lists as a possible cause of this error).

@mc-ct
Copy link

mc-ct commented Sep 27, 2022

@dking12 I've resolved my issue. In my case I'd missed the prerequisite of adding the app as an authorized app in the Box Admin Console Custom Apps Manager.

After doing that, I was able to generate tokens with this library using this:

const service = OAuth2.createService('Box')
    .setTokenUrl('https://api.box.com/oauth2/token')
    .setGrantType('client_credentials')
    .setParam('client_id', BOX_API_CLIENT_ID)
    .setParam('client_secret', BOX_API_CLIENT_SECRET)
    .setParam('box_subject_type', 'enterprise')
    .setParam('box_subject_id', BOX_ENTERPRISE_ID)
    .setPropertyStore(PropertiesService.getUserProperties());

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

4 participants