`getClientCredentialsToken()` sends `scopes` (comma-joined) instead of the OAuth `scope` parameter

Open Beginner friendly
#856 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
node.js, typescript

Research direction

Start with getClientCredentialsToken() in src/XeroClient.ts and inspect how tokenRequest() encodes its body. Check the README examples at lines 172 and 236 for the conflicting guidance. Done means the token request sends requested scopes as a space-delimited scope parameter and the README comment matches the behavior.

Written by the indexing model from the issue text.

Description

Which version of the SDK are you using?

20.3.0 (master at 538aa65)

Summary

getClientCredentialsToken() passes the configured scopes to the token endpoint under the key scopes:

https://github.com/XeroAPI/xero-node/blob/538aa65/src/XeroClient.ts#L196-L198

const { clientId, clientSecret, grantType, scopes } = this.config;
const result = await this.tokenRequest(clientId, clientSecret, { grant_type: grantType, scopes });

encodeBody() then runs encodeURIComponent on the array, which joins it with commas. The request body is:

POST https://identity.xero.com/connect/token
grant_type=client_credentials&scopes=accounting.transactions%2Caccounting.contacts

(captured by stubbing axios and calling getClientCredentialsToken() with scopes: ['accounting.transactions', 'accounting.contacts'])

The token endpoint expects the standard scope parameter, space-delimited (RFC 6749 §4.4.2). scopes isn't a token request parameter, so the requested scopes are not actually requested and the token falls back to whatever the connection grants by default. That breaks the README example that relies on requesting marketplace.billing for App Store calls (README L172). It also means an app can't narrow a custom-connection token to fewer scopes.

xero-python already does this correctly (post_data["scope"] = "marketplace.billing").

Suggested fix
const body: Record<string, string> = { grant_type: grantType };
if (scopes?.length) body.scope = scopes.join(' ');
const result = await this.tokenRequest(clientId, clientSecret, body);

The README comment at L236 (scopes ... // not used for client_credentials auth flow) contradicts L172 and could be updated at the same time.

Dominant language
TypeScript
Stars
264
Forks
177
Avg merge
10h 5m
Merged PRs (30d)
5

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from XeroAPI/xero-node

All issues in XeroAPI/xero-node

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.