News & Announcements User Community Developer Community

Welcome to the RingCentral Community

Please note the community is currently under maintenance and is read-only.

Search
Make sure to review our Terms of Use and Community Guidelines.
  Please note the community is currently under maintenance and is read-only.
Home » Developers
502 Bad Gateway when calling JWT Auth API
Tags: rest api, authentication
Feb 21, 2023 at 11:24am   •   1 replies  •  0 likes
Leigh M

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?

1 Answer
answered on Feb 21, 2023 at 1:51pm  

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}"))
    }

 0
on Feb 21, 2023 at 3:18pm   •  0 likes

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)

on Feb 21, 2023 at 4:26pm   •  1 likes

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}"))
}
on Feb 21, 2023 at 7:05pm   •  0 likes

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



A new Community is coming to RingCentral!

Posts are currently read-only as we transition into our new platform.

We thank you for your patience
during this downtime.

Try Workflow Builder

Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!

Try RingCentral Workflow Builder

PRODUCTS
RingEX
Message
Video
Phone
OPEN ECOSYSTEM
Developer Platform
APIs
Integrated Apps
App Gallery
Developer support
Games and rewards

RESOURCES
Resource center
Blog
Product Releases
Accessibility
QUICK LINKS
App Download
RingCentral App login
Admin Portal Login
Contact Sales
© 1999-2024 RingCentral, Inc. All rights reserved. Legal Privacy Notice Site Map Contact Us