question

Avijit Chauhan avatar image
Avijit Chauhan asked Phong Vu commented

OAU-213 TokenInvalid, while calling refresh token

We are using the OAuth Authorization Code flow, although while calling the refresh token, even when the refresh token is valid.

The access token is valid and works with other APIs when it is valid, although when we call API for a refresh token, it is giving us an invalid token.

I checked the forum for the solutions, although neither the token seems to be corrupted, nor the password has changed. Can you please assist us with the same?

I am attaching the code for the same below:

Authorization Code to get access token:

self.url = f"{self.API_Server_URL}/restapi/oauth/tok
clientId_clientSecret =f"{self.rc_clientId}:{self.rc_clientSecret}"
clientId_clientSecret_bytes = clientId_clientSecret.encode("ascii")
base64_bytes = base64.b64encode(clientId_clientSecret_bytes)
base64_clientId_clientSecret = base64_bytes.decode("ascii")

            headers = {
                "Authorization": f"Basic {base64_clientId_clientSecret}"
            }

            params = {
                "grant_type": "authorization_code",
                "code": code,
                "redirect_uri": self.redirect_uri + "/ringcentral/auth",
                "client_id": self.rc_clientId,
                "client_secret": self.rc_clientSecret,
                "refresh_token_ttl":604800
            }

            responseData = requests.post(self.url, headers=headers, data=params)


Code used for getting refresh token:

clientId_clientSecret = f"{rc_clientId}:{rc_clientSecret}"
 clientId_clientSecret_bytes = clientId_clientSecret.encode("ascii")
            base64_bytes = base64.b64encode(clientId_clientSecret_bytes)
            base64_clientId_clientSecret = base64_bytes.decode("ascii")
            brand_type = obj.get('brand_type')
            
            url = f"{API_Server_URL}/restapi/oauth/token"

            if brand_type:
                url = url.replace(".com", ".biz")

            headers = {
                "Content-Type": "application/x-www-form-urlencoded",
                "Accept": "application/json",
                "Authorization": f"Basic {base64_clientId_clientSecret}"
            }
            params = {
                "refresh_token": obj.get("refresh_token"),
                "grant_type": "refresh_token",
                "endpoint_id": obj.get("endpoint_id")
            }

            responseData = requests.post(url, headers=headers, data=params)
authenticationoauthget token
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Phong Vu avatar image
Phong Vu answered Phong Vu commented

Fix these first and run again to see if it helps

// Login body params
   params = {
                "grant_type": "authorization_code",
                "code": code,
                "redirect_uri": self.redirect_uri + "/ringcentral/auth"
                // "refresh_token_ttl":604800 // Unneccessary
            }
// Refresh body
   params = {
                "refresh_token": obj.get("refresh_token"),
                "grant_type": "refresh_token"               
            }
2 comments
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Avijit Chauhan avatar image Avijit Chauhan commented ·

I tired to change the params, although I am able to successfully authenticate, but while using refresh token I am still getting the same error.

0 Likes 0 ·
Phong Vu avatar image Phong Vu ♦♦ Avijit Chauhan commented ·

While this should not be the cause. But worth to check and change,

I don't know what brand do you use when you authenticate. But then you refresh, you check the brand and change the domain?

if brand_type:
    url = url.replace(".com", ".biz")

This is what I implemented in Node JS and it works perfectly.

var endpoint = '/restapi/oauth/token'
var basic = `${this.clientId}:${this.clientSecret}`;
var headers = {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Accept': 'application/json',
            'Authorization': 'Basic ' + Buffer.from(basic).toString('base64')
            };
var body = querystring.stringify({
            'grant_type' : 'refresh_token',
            'refresh_token' : tokens.refresh_token,
        });
0 Likes 0 ·

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys