Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hashir-dhattiwala opened this issue Jul 26, 2016 · 1 comment
Open

Comments

@hashir-dhattiwala
Copy link

hashir-dhattiwala commented Jul 26, 2016

@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....

@produktive
Copy link

@hashir-dhattiwala Did you resolve this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants