Skip to content

401 Unauthorized even if all necessary tokens passed as params #59

Open
@hashir-dhattiwala

Description

@hashir-dhattiwala

@adoy
@bachkoutou

<?php
require_once('Client.php');
require_once('Logging.php');
require_once('GrantType/IGrantType.php');
autoLoadAll();

const CLIENT_ID = '<client_id>';
const CLIENT_SECRET = '<client_secret>';
const REDIRECT_URI = 'https://webexpressen.no/oauth2/';
const AUTHORIZATION_ENDPOINT = 'https://auth-sandbox.test.vismaonline.com/eaccountingapi/oauth/authorize';
const TOKEN_ENDPOINT = 'https://auth-sandbox.test.vismaonline.com/eaccountingapi/oauth/token';

$log = new Logging();
#set path and name of log file (optional)
$log->lfile($_SERVER["DOCUMENT_ROOT"].'/oauth2/customPhp.log');
$client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET);
if(!isset($_GET['code']))
{
    $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
    $log->lwrite("\nAUTH_URL:".$auth_url);
    header('Location: ' . $auth_url);
    die('Redirect');
}
else
{
    session_start();
    $params = array('code' => $_REQUEST['code'], 'redirect_uri' => REDIRECT_URI);
    $_SESSION['params'] = $params;
    $response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $_SESSION['params']);
    $log->lwrite(print_r($response, true));
    $client->setAccessToken($response['result']['access_token']);

    $_SESSION['params']['refresh_token'] = $response['result']['refresh_token'];
    $_SESSION['headers']['client_id'] = CLIENT_ID;
    $_SESSION['headers']['client_secret'] = CLIENT_SECRET;
    $_SESSION['headers']['scope'] = "accounting+accounting_readonly";
    $_SESSION['headers']['redirect_uri'] = REDIRECT_URI;
    $_SESSION['headers']['refresh_token'] = $response['result']['refresh_token'];

    $atResponse = $client->getAccessToken(TOKEN_ENDPOINT, 'refresh_token', $_SESSION['headers']);
    //Get the data from given external resource by `fetch` method of Client.php
    $_SESSION['params']['includeZeroBalance'] = true;
    echo "<pre/>All session values";print_r($_SESSION);
    $dataEndpoint = "https://eaccountingapi-sandbox.test.vismaonline.com/v1/accountbalances/2016-01-15";
    $accountData = $client->fetch($dataEndpoint, $_SESSION['params'], 'GET', $_SESSION['headers']);
    echo "<pre/>Account Data:";print_r($accountData);
}
// close log file
$log->lclose();

function autoLoadAll() {
    $scan = scandir('GrantType/');
    foreach ($scan as $class) {
        if (strpos($class, '.php') !== false && strpos($class, 'IGrantType') === false) {
            require_once('GrantType/' . $class);
            #include_once('GrantType/' . $class);
        }
    }
    return true;
}

Please look at the above script I have built for calling an external api by passing through Oauth2 Authentication Mechanism using your Client Library.

As you can see I have passed all necessary tokens and parameters properly in standard format but still when I run the given script I get below message:

Array
(
    [result] => Array
        (
            [Message] => Unauthorized
        )

    [code] => 401
    [content_type] => application/json; charset=utf-8
)

Below is a reference link about webservice documentation.
https://developer.vismaonline.com/ - I am calling the very first webservice /v1/accountbalances/{date}

Can someone assist me about what could be wrong, I can disclose only this much code.

Waiting....

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions