# 500 Internal Server Error when trying to get access and refresh token

**URL:** <https://community.hubspot.com/t/500-internal-server-error-when-trying-to-get-access-and-refresh-token/12765>\
**Category:** APIs & Integrations\
**Tags:** oauth\
**Created:** [February 5, 2019, 12:01am UTC](https://community.hubspot.com/t/500-internal-server-error-when-trying-to-get-access-and-refresh-token/12765 "2019-02-05T00:01:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suryatapa](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/suryatapa/32/73373_2.png) [@Suryatapa](https://community.hubspot.com/u/Suryatapa)\
**Post date:** [February 5, 2019, 12:01am UTC](https://community.hubspot.com/t/500-internal-server-error-when-trying-to-get-access-and-refresh-token/12765/1 "2019-02-05T00:01:24Z")

</div>

Hi

I had the oauth process working till a fews weeks back - access and refresh tokens were getting updated. But, now if I try to get or refresh tokens I get 500 internal server error. I get the grant code for a new oauth but the subsequent access token generation fails.The same code was working a while back. What could be the reason?

---

<div class="post-metadata">

**Author:** ![cbarley](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/cbarley/32/82559_2.png) [@cbarley](https://community.hubspot.com/u/cbarley)\
**Post date:** [February 5, 2019, 8:49pm UTC](https://community.hubspot.com/t/500-internal-server-error-when-trying-to-get-access-and-refresh-token/12765/2 "2019-02-05T20:49:32Z")

</div>

Hi @Suryatapa , happy to take a look here. Can you clarify what step exactly you’re getting caught up on in the OAuth flow? It sounds like you’re getting the 500 error using this endpoint: [Accounts Dashboard | HubSpot](https://developers.hubspot.com/docs/methods/oauth2/get-access-and-refresh-tokens). If this is the case, can you please provide the following:

- Your App ID
- An example request that’s failing.
- The _full_ error response you’re getting from HubSpot’s servers.

Thank you!

---

<div class="post-metadata">

**Author:** ![Suryatapa](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/suryatapa/32/73373_2.png) [@Suryatapa](https://community.hubspot.com/u/Suryatapa)\
**Post date:** [February 5, 2019, 9:04pm UTC](https://community.hubspot.com/t/500-internal-server-error-when-trying-to-get-access-and-refresh-token/12765/3 "2019-02-05T21:04:43Z")

</div>

Thank you for your reply.Please find the below information.

APP Id: 188139

Request Code:

RestTemplate restTemplate = new RestTemplate();  
HttpHeaders headers = new HttpHeaders();  
headers.setContentType(MediaType.APPLICATION\_FORM\_URLENCODED);  
MultiValueMap\<String, String\> map = new LinkedMultiValueMap\<\>();  
map.add(“grant\_type”,“authorization\_code”);  
map.add(“client\_id”, MY\_CLIENT\_ID);  
map.add(“client\_secret”,MY\_CLIENT\_SECRET);  
map.add(“redirect\_uri”,“[http://localhost:9090/api/v1/projects/newhubspotOauth2redirect](http://localhost:9090/api/v1/projects/newhubspotOauth2redirect)");  
map.add(“code”, RECEIVED\_GRANT\_CODE);

String url = “[https://api.hubapi.com/oauth/v1/token”](https://api.hubapi.com/oauth/v1/token%E2%80%9D);  
HttpEntity\<MultiValueMap\<String, String\>\> request = new HttpEntity\<\>(map, headers);  
ResponseEntity\<String\> responseToken = restTemplate.exchange(url, HttpMethod.POST, request,String.class);

The last line throws the below error:

org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error

---

<div class="post-metadata">

**Author:** ![cbarley](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/cbarley/32/82559_2.png) [@cbarley](https://community.hubspot.com/u/cbarley)\
**Post date:** [February 6, 2019, 3:09pm UTC](https://community.hubspot.com/t/500-internal-server-error-when-trying-to-get-access-and-refresh-token/12765/4 "2019-02-06T15:09:00Z")

</div>

Hi @Suryatapa , that 500 error looks like it’s coming from your program. I’ll need to see an error coming from HubSpot’s servers in order to accurately diagnose the issue. It would also be helpful if I could get the raw request to HubSpot’s servers that’s causing the error. I won’t be able to do much just taking a look at your code without the ability to run it.

HubSpot errors follow the format of:

```auto
{
   status: 'error',
   message: MESSAGE HERE,
   correlationId: ID HERE,
   requestId: ID HERE
}

```

Thank you

---

<div class="post-metadata">

**Author:** ![Suryatapa](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/suryatapa/32/73373_2.png) [@Suryatapa](https://community.hubspot.com/u/Suryatapa)\
**Post date:** [February 6, 2019, 6:50pm UTC](https://community.hubspot.com/t/500-internal-server-error-when-trying-to-get-access-and-refresh-token/12765/5 "2019-02-06T18:50:02Z")

</div>

I get the 500 internal error when I make the call using postman as well.

This is the error response:

{  
“status”: “error”,  
“message”: “internal error”,  
“correlationId”: “9f174a19-ed9a-457d-98dd-9a2584848d51”,  
“requestId”: “52ea86605f39eab7f585fb5ccc15747d”  
}

---

<div class="post-metadata">

**Author:** ![cbarley](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/cbarley/32/82559_2.png) [@cbarley](https://community.hubspot.com/u/cbarley)\
**Post date:** [February 6, 2019, 9:38pm UTC](https://community.hubspot.com/t/500-internal-server-error-when-trying-to-get-access-and-refresh-token/12765/6 "2019-02-06T21:38:21Z")

</div>

Great, thanks! Which endpoint are you using? Can you provide the request that you’re making? Make sure you follow the 4 steps outlined [here](https://developers.hubspot.com/docs/methods/oauth2/oauth2-overview). An example of programmatic fetching of tokens can be found in the[Oauth quickstart guide](https://developers.hubspot.com/docs/methods/oauth2/oauth2-quickstart) as well

---

<div class="post-metadata">

**Author:** ![Suryatapa](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/suryatapa/32/73373_2.png) [@Suryatapa](https://community.hubspot.com/u/Suryatapa)\
**Post date:** [February 7, 2019, 7:31am UTC](https://community.hubspot.com/t/500-internal-server-error-when-trying-to-get-access-and-refresh-token/12765/7 "2019-02-07T07:31:07Z")

</div>

[https://app.hubspot.com/oauth/authorize?client\_id=1342ad9b-b2b4-4ae5-a9f9-88e9b81a63e0&redirect\_uri=](https://app.hubspot.com/oauth/authorize?client_id=1342ad9b-b2b4-4ae5-a9f9-88e9b81a63e0&redirect_uri=)“[http://localhost:9090/api/v1/projects/newhubspotOauth2redirect](http://localhost:9090/api/v1/projects/newhubspotOauth2redirect)"&scope=contacts%20automation%20content%20business-intelligence%20e-commerce%20files%20forms%20hubdb%20integration-sync%20social%20tickets%20timeline%20actions%20reports

- This returns a grant code.

Then the grant code is set in the code i shared earlier to fetch the tokens from

`https://api.hubapi.com/oauth/v1/token`

And this process was working fine until it stopped working.

I am wondering if it is something to do with the test account that is logging in for authentication - maybe it exceeded its no of attempts or something like that.

---

<div class="post-metadata">

**Author:** ![cbarley](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/cbarley/32/82559_2.png) [@cbarley](https://community.hubspot.com/u/cbarley)\
**Post date:** [February 7, 2019, 7:01pm UTC](https://community.hubspot.com/t/500-internal-server-error-when-trying-to-get-access-and-refresh-token/12765/8 "2019-02-07T19:01:10Z")

</div>

Hi @Suryatapa , I’m able to reproduce the error with your app. I created another app in your Developer Account that I was able to connect to your test account successfully via OAuth. I’m not sure why that error is occurring at the moment. One working theory is that there were many authorization URLs created for this single app to attempt to connect to HubSpot which could have thrown it off. Is your app installed in any other accounts? If not, I’d suggest making another app in your developer account, and starting fresh.

Make sure that your redirect\_uri in your authorization URL does not contain quotation marks.

---

<div class="post-metadata">

**Author:** ![Suryatapa](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/suryatapa/32/73373_2.png) [@Suryatapa](https://community.hubspot.com/u/Suryatapa)\
**Post date:** [February 7, 2019, 9:48pm UTC](https://community.hubspot.com/t/500-internal-server-error-when-trying-to-get-access-and-refresh-token/12765/9 "2019-02-07T21:48:27Z")

</div>

Thanks a lot for all your help!!!
