nylas-samples / nylas-hacktoberfest-2023

Build with Nylas
MIT License
8 stars 13 forks source link

Using Nylas Node v3 SDK, create a code sample to create a user grant #7

Closed relaxedtomato closed 10 months ago

relaxedtomato commented 11 months ago

Using Nylas Node v3 SDK, create a code sample to create a user grant

amit-ksh commented 11 months ago

Hey @relaxedtomato, I would like to work on this one.

relaxedtomato commented 11 months ago

@amit-ksh awesome - connect if you have questions. We can get you setup with our new dashboard for testing purposes.

Send your email over to devrel at nylas.com with the subject: hacktoberfest23, and we'll quickly send account details 👍

amit-ksh commented 11 months ago

@relaxedtomato, I'm having some trouble creating grants using the nylas.auth.grants.create method but it requires a refreshToken. So, I attempted to obtain a refreshToken using the nylas.auth.exchangeCodeForToken, but unfortunately, it resulted in an error. Could you please assist me with this issue?

My Code-

const { code } = req.query;
const resp = await nylas.auth.exchangeCodeForToken({
  code,
});

Error I got when exchangeCodeForToken is called-

/nylas-hacktoberfest-2023/code-samples/grant/node/create/node_modules/nylas/lib/esm/apiClient.js:98
error = new NylasOAuthError(camelCaseError, response.status);
                            ^

NylasOAuthError: Internal error, contact administrator
    at APIClient.request (file:///D:/personal/open-source/nylas-hacktoberfest-2023/code-samples/grant/node/create/node_modules/nylas/lib/esm/apiClient.js:98:29)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async file:///D:/personal/open-source/nylas-hacktoberfest-2023/code-samples/grant/node/create/index.js:28:16 {
  error: 'internal_error',
  errorCode: 500,
  errorDescription: 'Internal error, contact administrator',
  errorUri: 'https://accounts.nylas.io/#tag/Event-Codes',
  statusCode: 500
}
relaxedtomato commented 11 months ago

@amit-ksh - will take a look today 👍

relaxedtomato commented 11 months ago

@amit-ksh, just confirming two items:

are you able to share a code sample or branch that I can take a look at?

relaxedtomato commented 11 months ago

So from the SDK docs, exchangeCodeForToken doesn't exist:

Screenshot 2023-10-18 at 1 29 01 PM
amit-ksh commented 11 months ago

Code: https://github.com/amit-ksh/nylas-hacktoberfest-2023/tree/create-grants/code-samples/grant/node/create-grants

relaxedtomato commented 11 months ago

@amit-ksh - sorry, just realized I confused languages, let me take another look.

relaxedtomato commented 11 months ago

Here is the Node SDK Reference: https://nylas-nodejs-sdk-reference.pages.dev/

relaxedtomato commented 11 months ago

@amit-ksh:

I noticed a few items:

I see, this accesses the nylas.grants public method. So I have one thought, once you authenticate a user, does the following list grants?

Once we authenticate a user, a grant should be created for them. You can test this out using the dashboard as well.

amit-ksh commented 11 months ago

hey @relaxedtomato! I'm getting the error on this line const resp = await nylas.auth.exchangeCodeForToken. It returned the 500 status when I did some debugging.

I believe this line const newGrants = await nylas.auth.grants.create is not causing the issue.

Also, I tried creating grants using the dashboard it's working as expected as well as this method nylas.auth.grants.list.

relaxedtomato commented 11 months ago

Okay, let me try this out shortly!

amit-ksh commented 11 months ago

I tried to pass all the arguments that I have to exchangeCodeForToken method and now it's working I'm not getting `refreshToken in response.

const resp = await nylas.auth.exchangeCodeForToken({
      code,
      clientSecret: process.env.CLIENT_SECRET,
      clientId: process.env.CLIENT_ID,
      redirectUri: "http://localhost:3000/oauth/exchange",
    });

Response of exchangeCodeForToken function-

{
  accessToken: 'token',
  grantId: 'id',
  email: 'some@gmail.com',
  expiresIn: 3600,
  idToken: 'token',
  tokenType: 'Bearer',
  scope: 'scope1 scope2'
}

Also, when I list grants using this nylas.auth.grants.list, I'm getting all the emails I authenticated to but the same is not shown in the Nylas Dashboard.

relaxedtomato commented 11 months ago

@amit-ksh - this looks good, since I see that you are receiving the grandId in return which can be used for subsequent requests.

Let's prepare a PR and I will review 👍

relaxedtomato commented 10 months ago

Okay, let me take a look at the PR!