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
How to use JWT in HTTP Request
Tags: rest api
May 4, 2023 at 5:13pm   •   1 replies  •  0 likes
Erin Lumsden

I have successfully got a VBA script to request Call Logs for my company. However, I have been using the Bearer token that is generated by the 'Try It Out' button. I want to run this script automatically so I generated a JWT Token and added it into my requestHeader as the token generated seems to expire after an hour or so. I then switched my app to use JWT auth flow, however, it is not quite right and now I can't switch back to Password-based for testing. Please let me know what I am missing from my code.

EDIT: I tried with curl and it gives me OAU-149 Invalid token. I have double checked that it copied correctly and even generated a new JWT token. Neither work in curl either :(


dateVars = Format(Date - 1, "YYYY-MM-DD") 
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    Url = "https://platform.ringcentral.com/restapi/v1.0/account/~/call-log?direction=Inbound&direction=Outbound&type=Voice&view=Detailed&withRecording=false&dateFrom=" & dateVars & "T14%3A00%3A00.000Z&dateTo=" & dateVars & "T13%3A59%3A00.000Z&page=1&perPage=100"
    objHTTP.Open "GET", Url, False
    objHTTP.setRequestHeader "accept", "application/json"
    objHTTP.setRequestHeader "authorization", "Bearer <JWT TOKEN HERE>"
1 Answer
answered on Jun 16, 2023 at 12:54pm  

RingCentral API documentation is the worst documentation by a major company that I have ever encountered. So many circular references and ambiguous language, it is so frustrating to try to figure out how this shit works! Can't RC hire a competent documentation writer?

For example, this is a direct quote from a documentation page: "The key to groking how RingCentral uses JWTs is in understanding that JWTs are not used directly to call the API."

WTF is "groking"?

We are trying to figure out this JWT Authentication workflow, which, to restate my frustration, makes no sense. Documentation states that the JWT has to be created by a user, but only users who have access to the Dev Console can create JWT. How, then, are we supposed to create JWT for the other users? In the Password Flow, we have to provide username and password to authenticate the user and obtain a token for that user, but in JWT there is no way to identify which user is requesting the token. I cannot find any information about how this is handled.


 0
answered on May 8, 2023 at 5:53pm  

Thanks @Phong Vu. Are you able to check my ClientID and Client Secret from your end? I have followed your tutorial but even in php or using curl in terminal I am getting AGW-402 Invalid Authorization


 0
answered on May 5, 2023 at 7:30am  

What you tried was incorrect. The JWT token is not an access token, but it is a user credential that you can use to get the access token and refresh token instead of using the user username and password. If you understand PHP, you can convert this function to VBA script for your app.

private function authenticate(){
      $url = $_ENV["RC_SERVER_URL"] . "/restapi/oauth/token";
      $basic = $_ENV["RC_CLIENT_ID"] .":". $_ENV["RC_CLIENT_SECRET"];
      $headers = array (
              'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
              'Accept: application/json',
              'Authorization: Basic '.base64_encode($basic)
            );
      $body = http_build_query(array (
              'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
              'assertion' => $_ENV["JWT_TOKEN"]
            ));
      try {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 600);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

        $strResponse = curl_exec($ch);
        $curlErrno = curl_errno($ch);
        if ($curlErrno) {
          throw new Exception($curlErrno);
        } else {
          $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
          curl_close($ch);
          if ($httpCode == 200) {
            $date = new DateTime();
            $jsonObj = json_decode($strResponse);
            $tokensObj = array(
              "tokens" => $jsonObj,
              "timestamp" => $date->getTimestamp()
            );
            // Logged in successfully => Save tokens in session or write to file/database for reuse
            $_SESSION['sessionAccessToken'] =  json_encode($tokensObj, JSON_PRETTY_PRINT);
            return;
          }else{
            throw new Exception($strResponse);
          }
        }
      } catch (Exception $e) {
        throw $e;
      }
    }

You can get the entire PHP project from my GitHub repo to learn more about how to use the refresh token to get a new access token if it expires.


 0



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