# Invalid\_grant error

**URL:** https://forums.nylas.com/t/invalid-grant-error/163
**Category:** Question and Answers
**Tags:** email-api, authentication
**Created:** [April 11, 2024, 5:19pm UTC](https://forums.nylas.com/t/invalid-grant-error/163 "2024-04-11T17:19:40Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ikram](https://avatars.discourse-cdn.com/v4/letter/i/7c8e57/32.png) [@ikram](https://forums.nylas.com/u/ikram)
#### Post date: [April 11, 2024, 5:19pm UTC](https://forums.nylas.com/t/invalid-grant-error/163/1 "2024-04-11T17:19:40Z")

</div>

Hi,

I’m following [these steps](https://developer.nylas.com/docs/v3/auth/hosted-oauth-apikey/#start-an-authorization-request) in v3 API and I’m unable to get the grant ID through API. This is the flow we are trying to achieve.

{baseurl}/v3/connect/auth — calling this API and it takes through the google/outlook auth flow

redirection is happening to the call back URL with a code. And when I call the “{baseurl}/v3/connect/token” API getting this error. In barely less than few seconds on generating the code, unsure how it’s expired.

Since I’m unable to get the grant ID even once, not being able to list the messages or access contents from email provider.

```auto
{
  "error": "invalid_grant",
  "error_code": 45004,
  "error_description": "Given 'code' not valid or expired",
  "error_uri": "https://accounts.nylas.io/#tag/Event-Codes",
  "request_id": "5143c462-c6d9-4eac-a2ca-38030c02650b"
}

```

My request does contain all the parameters recommended in the docs.

```auto
{
  "client_id": "<NYLAS_CLIENT_ID>",
  "client_secret": "<NYLAS_API_KEY>",
  "grant_type": "authorization_code",
  "code": "string",
  "redirect_uri": "https://example.com/callback-handler",
  "code_verifier": "nylas"
}

```

---

<div class="post-metadata">

### Author: ![Blag](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.nylas.com/blag/32/9_2.png) [@Blag](https://forums.nylas.com/u/Blag)
#### Post date: [April 11, 2024, 7:48pm UTC](https://forums.nylas.com/t/invalid-grant-error/163/2 "2024-04-11T19:48:48Z")

</div>

Hello @ikram Which SDK are you using? and which scenario are your working on?

---

<div class="post-metadata">

### Author: ![ikram](https://avatars.discourse-cdn.com/v4/letter/i/7c8e57/32.png) [@ikram](https://forums.nylas.com/u/ikram)
#### Post date: [April 11, 2024, 8:55pm UTC](https://forums.nylas.com/t/invalid-grant-error/163/3 "2024-04-11T20:55:19Z")

</div>

Hi @Blag! Thank you for asking.

I’m using the Nylas V3 API that’s documented in Postman through our PHP code base. We are building an email client within our application and trying to let users onboard. This is the user flow

1. User enters their email id in our application.
2. We call `https://api.us.nylas.com/v3/providers/detect` API to find the provider (works fine)
3. We call `https://api.us.nylas.com/v3/connect/auth` with required information and it redirects us to the concerned providers auth flow (works fine)
4. User completes the auth. flow and get redirected to our callback url with a code. By now, user returned to our application with {callbackUrl}/login?code= (supposedly working fine)
5. We call `https://api.us.nylas.com/v3/connect/token` with the above mentioned information to get the grant id, so that we could save it and give user the access to their email contents (this is where it’s not working and we are getting the above error)

Without grant id, we are unable to let users access their email contents in our application. Appreciate your support. Thank you!

---

<div class="post-metadata">

### Author: ![Blag](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.nylas.com/blag/32/9_2.png) [@Blag](https://forums.nylas.com/u/Blag)
#### Post date: [April 12, 2024, 12:19pm UTC](https://forums.nylas.com/t/invalid-grant-error/163/4 "2024-04-12T12:19:57Z")

</div>

Hello @ikram

I have done a lot of Hosted Auth with our SDKs (Ruby, Python, Java and Kotlin) but I must admit that I have never called the APIs directly.

I’m not a PHP expert but came up with this yesterday night 😁 You should be able to see what you are missing 😉 It’s actually way easier than expected 🤔

[php-email-reader-auth](https://github.com/nylas-samples/php-email-reader-auth)

Let me know if you need anything else…
