question

Leigh M avatar image
Leigh M asked Leigh M commented

502 Bad Gateway when calling JWT Auth API

I keep getting a 502 Bad Gateway error when attempting to obtain an access token using JWT, but am not sure why this is happening.


Powershell code:

$RC_SERVER_URL="https://platform.devtest.ringcentral.com"
$RC_CLIENT_ID="T2......" # Copied from the app I created
$RC_CLIENT_SECRET="v8d......" # Copied from the app I created
$RC_JWT_TOKEN = "eyJra....." # Created under My Account > Credentials and given specific authorization to the app Client ID above

# as per https://developers.ringcentral.com/guide/authentication/jwt-flow#technical-discussion
$splat = @{
    Method  = "POST"
    Uri     = $RC_SERVER_URL + "/restapi/oauth/token"
    ContentType = 'application/x-www-form-urlencoded'
    headers = @{   
        Accept = 'application/json'
        Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes("${RC_CLIENT_ID}:${RC_CLIENT_SECRET}"))
    }
    body    = @{
        grant_type = 'urn:ietf:params:oauth:grant-type:jwt-bearer'
        assertion = $RC_JWT_TOKEN
    }
}

$Response = Invoke-RestMethod @splat


The object looks good and isn't missing anything:

$splat
Name                           Value
----                           -----
ContentType                    application/x-www-form-urlencoded
Method                         POST
body                           {assertion, grant_type}
headers                        {Accept, Authorization}
Uri                            https://platform.devtest.ringcentral.com/restapi/oauth/token

PS C:\scripts\Get-RingCentralData> $splat.headers
Name                           Value
----                           -----
Accept                         application/json
Authorization                  Basic VAAyAF...

PS C:\scripts\Get-RingCentralData> $splat.body
Name                           Value
----                           -----
assertion                      eyJra...
grant_type                     urn:ietf:params:oauth:grant-type:jwt-bearer


The response I get when executing this is:

$Response = Invoke-RestMethod @splat
Invoke-RestMethod : The remote server returned an error: (502) Bad Gateway.
At line:1 char:13
+ $Response = Invoke-RestMethod @splat
+             ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand


Any ideas why this might be happening?

rest apiauthentication
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 Leigh M commented

I am not an expert in Powershell. But, why the content-type is not in the headers?

ContentType = 'application/x-www-form-urlencoded'
    headers = @{   
        Accept = 'application/json'
        Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes("${RC_CLIENT_ID}:${RC_CLIENT_SECRET}"))
    }

Should it be like this?

headers = @{   
        ContentType = 'application/x-www-form-urlencoded'
        Accept = 'application/json'
        Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes("${RC_CLIENT_ID}:${RC_CLIENT_SECRET}"))
    }
3 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.

Leigh M avatar image Leigh M commented ·

Powershell's Invoke-RestMethod cmdlet has a -ContentType argument. It effectively puts it into the headers for you.

I just tried inserting it directly into the headers instead of using the -ContentType argument, but the result is the same (502 Bad Gateway)

0 Likes 0 ·
Phong Vu avatar image Phong Vu ♦♦ Leigh M commented ·

I guess now it's because of wrong header encoding. Try this

headers = @{   
    ContentType = 'application/x-www-form-urlencoded; charset=UTF-8'
    Accept = 'application/json'
    Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("${RC_CLIENT_ID}:${RC_CLIENT_SECRET}"))
}
1 Like 1 ·
Leigh M avatar image Leigh M Phong Vu ♦♦ commented ·

Changing to UTF8 solved the issue. Thanks a lot, Phong Vu!

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