Skip to main content
The First Step
October 19, 2021
Question

Invalid Authorization header AGW-402

I have a standard app that is using webhook subscription and read presence permissions, I am getting below since yesterday

[errorCode] => AGW-402

[message] => Invalid Authorization header

tried new app too but its not registering calls using the same old process that worked for years.

Can anybody help ?

    4 replies

    The First Step
    October 19, 2021

    It seemed you could not pass the authentication and its failing to authenticate.

    Could you please add some more details?

    The First Step
    October 19, 2021

    Thank you for your reply, we re subscribe it once its expired as it does everymonth, use following to link and then trigger webhook

    function subscribe($token){

    $url= BASE_URL."/restapi/v1.0/subscription";

    $headers = array();

    $headers[] = "Content-Type: application/json";

    $headers[] = "Authorization: Bearer ".$token;

    $headers[] = "Accept: application/json";

    $data = array("eventFilters"=>array("/restapi/v1.0/account/~/presence?detailedTelephonyState=true"), "deliveryMode"=>array( "transportType"=> "WebHook","address"=>"https://awplat.tbmslive.com/cli/png/webhook.php"));

    $response = curlPost($url, $headers, json_encode($data), true);

    return $response;

    The First Step
    October 22, 2021

    <?php

    define("BASE_URL","https://platform.ringcentral.com");

    //define("BASE_URL","https://platform.devtest.ringcentral.com");

    define("CLIENT_ID","xXXXXXxxxXXXXXXXXx");

    define("APP_SECRET","dddddddXXXXXXXXxxxxxxxXXXXXXXXXXxxxxxxxxXXXXXX");

    define("RED_URI","https://tbmslive.com/ringcentral/platinum/process.php");



    <?php

    require_once("config.php");

    if(file_exists("token.json") && file_get_contents("token.json") != ""){

    $tok_time = file_get_contents("token_expiry.txt");

    $ref_time = file_get_contents("refresh_token_expiry.txt");

    if(time() > $tok_time && time() > $ref_time){

    unlink("token.json");

    unlink("token_expiry.txt");

    unlink("refresh_token_expiry.txt");

    $url = BASE_URL."/restapi/oauth/authorize?response_type=code&client_id=".CLIENT_ID."&redirect_uri=".RED_URI;

    header("Location: ".$url);

    exit();

    }else if(time() > $tok_time && time() < $ref_time){

    $response = json_decode(file_get_contents("token.json"), true);

    $refresh_token = $response['refresh_token'];

    $response = get_Access_token_by_refresh_token($refresh_token);

    $token = $response['access_token'];

    file_put_contents("token.json", json_encode($response));

    file_put_contents("token_expiry.txt", time() + $response['expires_in']);

    file_put_contents("refresh_token_expiry.txt", time() + $response['refresh_token_expires_in']);

    }else{

    $response = json_decode(file_get_contents("token.json"), true);

    $token = $response['access_token'];

    }

    }else{

    if(isset($_GET['code']) && $_GET['code'] != ""){

    $code = $_GET['code'];

    $url = BASE_URL."/restapi/oauth/token";

    $headers = array();

    $headers[] = "Content-Type: application/x-www-form-urlencoded";

    $data = array(

    "grant_type" => "authorization_code",

    "code" => $code,

    "redirect_uri" => RED_URI,

    "client_id"=> CLIENT_ID

    );

    $response = curlPost($url, $headers, $data, false);

    $token = $response['access_token'];

    file_put_contents("token.json", json_encode($response));

    file_put_contents("token_expiry.txt", time() + $response['expires_in']);

    file_put_contents("refresh_token_expiry.txt", time() + $response['refresh_token_expires_in']);

    $sub = 2 * 24 * 60 * 60;

    $expiry = $response['refresh_token_expires_in'] - $sub;

    file_put_contents("expiry.txt", time() + $expiry);

    $url = "http" . (($_SERVER['SERVER_PORT'] == 443) ? "s://" : "://") . $_SERVER['HTTP_HOST'] . $_SERVER["PHP_SELF"];

    header("Location: ".$url);

    }else{

    $url = BASE_URL."/restapi/oauth/authorize?response_type=code&client_id=".CLIENT_ID."&redirect_uri=".RED_URI;

    header("Location: ".$url);

    exit();

    }

    }

    echo "<pre>";

    //$response = subscribe($token);

    //print_r($response);

    $response = getSubscription($token);

    print_r($response);

    mysql_close();



    function curlPost($url, $header, $data, $type){

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL,$url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

    curl_setopt($ch, CURLOPT_POST, true);

    if($type){

    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    }else{

    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

    }

    $result=curl_exec ($ch);

    $status_code = curl_getinfo($ch);

    curl_close ($ch);

    $responce = json_decode($result,true);

    return($responce);

    }


    function curlGet($url, $header){

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL,$url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

    $result=curl_exec ($ch);

    $status_code = curl_getinfo($ch);

    curl_close ($ch);

    $responce = json_decode($result,true);

    return($responce);

    }

    function get_Access_token_by_refresh_token($get_refresh_token){

    $url = BASE_URL."/restapi/oauth/token";

    $headers = array();

    $auth = base64_encode(CLIENT_ID .":". APP_SECRET);

    $headers[] = "Content-Type: application/x-www-form-urlencoded";

    $headers[] = "Authorization: Basic ".$auth;

    $data = array(

    "grant_type" => "refresh_token",

    "refresh_token" => $get_refresh_token

    );

    $response = curlPost($url, $headers, $data, false);

    return $response;

    }

    /*

    Get Token

    */


    function subscribe($token){

    $url= BASE_URL."/restapi/v1.0/subscription";

    $headers = array();

    $headers[] = "Content-Type: application/json";

    $headers[] = "Authorization: Bearer ".$token;

    $headers[] = "Accept: application/json";

    $data = array("eventFilters"=>array("/restapi/v1.0/account/~/presence?detailedTelephonyState=true"), "deliveryMode"=>array( "transportType"=> "WebHook","address"=>"https://tbmslive.com/ringcentral/platinum/webhook.php"));

    $response = curlPost($url, $headers, json_encode($data), true);

    return $response;

    }


    function readPresence($token){

    $url= BASE_URL."/restapi/v1.0/account/~/extension/~/presence";

    $headers = array();

    $headers[] = "Content-Type: application/json";

    $headers[] = "Authorization: Bearer ".$token;

    $headers[] = "Accept: application/json";

    $response = curlGet($url, $headers);

    return $response;

    }


    function getSubscription($token){

    $url= BASE_URL."/restapi/v1.0/subscription";

    $headers = array();

    $headers[] = "Content-Type: application/json";

    $headers[] = "Authorization: Bearer ".$token;

    $headers[] = "Accept: application/json";

    $response = curlGet($url, $headers);

    return $response;

    }

    function renewSubscription($token, $id){

    $url= BASE_URL."/restapi/v1.0/subscription/{subscriptionId}/renew";

    $headers = array();

    $headers[] = "Content-Type: application/json";

    $headers[] = "Authorization: Bearer ".$token;

    $headers[] = "Accept: application/json";

    $data = array();

    $response = curlPost($url, $headers, json_encode($data), true);

    return $response;

    }

    PhongVu
    Community Manager
    Community Manager
    October 22, 2021

    Do you mind to put code in code block. Bad question description and messy snippet code make it hard to help and thus, will not be answered!