How do I refresh a token
Asked by Arun sanap:

- My first access token get expired, now I want a new token without any User interface.

- I tried below in terminal,

    curl -X POST -u "{key}:{secret}" https://bitbucket.org/site/oauth2/access_token -d 
    grant_type=refresh_token -d refresh_token={refresh_token}

but getting, below error

{"error_description": "Invalid refresh_token", "error": "invalid_request"}

Questions :

1. What parameter should we pass to ***refresh_token***? we need a new access token then why need this field?

I tried, expired token and new access token(created using UI) as well but it not worked

Please help me to understand what parameter should we pass to the ***refresh_token***

And how can I generate new token using terminal/curl

Thanks


Answers:
author: Theodora Boudale
body: Hi Arun,

When you generated your very first access token with a command like

```
curl -X POST -u "{key}:{secret}" https://bitbucket.org/site/oauth2/access_token -d grant_type=authorization_code -d code={code}
```

the output should have been like this:

```
{
"access_token" : 5cEig-w1wXrlbM876XY1Nu-w5WfObqP0EOYo2JOPzs615QxZTqI9z8Gsy_21yX23Uspwblt8eKeBAcwpCrHY=,
"scopes": "repository",
"expires_in" : 7200,
"refresh_token" : 4Etiw6UP808AU3rIoUW,
"token_type" : bearer
}
```

You need to use the value of the field 'refresh_token' above in your call.

Afterwards, when you execute the following command with this value for the refresh token

```
curl -X POST -u "{key}:{secret}" https://bitbucket.org/site/oauth2/access_token -d grant_type=refresh_token -d refresh_token={refresh_token}
```

you will get in the output a new authorization code, and a new refresh token which you can use after the 2 hours expiry, and so on.

Please feel free to let me know if you have any questions.

Kind regards,  
Theodora

comments: author: Arun sanap
body: Hi Theodora

Thanks for quick response.

```
curl -X POST -u "{key}:{secret}" https://bitbucket.org/site/oauth2/access_token -d grant_type=authorization_code -d code={code}
```

When we are using the {code}, parameter do we have any expiry for it? Because it seems get invalid once get used.

{"error_description": "The specified code is not valid.", "error": "invalid_grant"}


author: Theodora Boudale
body: Hi Arun,

The following command

```
curl -X POST -u "{key}:{secret}" https://bitbucket.org/site/oauth2/access_token -d grant_type=authorization_code -d code={code}
```

you only use it ++once++, in the beginning.

After the first time you use it, you execute the other command:

```
curl -X POST -u "{key}:{secret}" https://bitbucket.org/site/oauth2/access_token -d grant_type=refresh_token -d refresh_token={refresh_token}
```

After the 2 hours expiry, you also use

```
curl -X POST -u "{key}:{secret}" https://bitbucket.org/site/oauth2/access_token -d grant_type=refresh_token -d refresh_token={refresh_token}
```

This will give you both an authorization code ++and++ a refresh token to use the next time you execute it.

Does this help?

Kind regards,  
Theodora


author: Arun sanap
body: It Help me lot.

Thanks Theodora


author: Yong
body: when executing

```
curl -X POST -u "{key}:{secret}" https://bitbucket.org/site/oauth2/access_token -d grant_type=authorization_code -d code={code}
```

{"error": "invalid_grant", "error_description": "The specified code is not valid."}

what's the problem then?


Tags: bitbucket-cloud