OAuth令牌不存在 - 401

时间:2016-04-23 17:09:18

标签: php oauth

我现在已经处理了以下问题几个小时了,这让我发疯了。

我们有一个由第三方开发的模块,该模块到目前为止工作正常,用于连接时间标签,这样就可以为我们的志愿者请求日历。

使用此第三方公司提供的手册,此错误有一部分" 401 - OAuth令牌不存在"

他们在评论这些内容:

//if (!$token) {
    $client->authenticate();
    $client->setToken(reformatToken($client->getToken()));
//} else {
//   $client->setToken($token);
//}

然后注销Google,浏览网站上的组件,授予访问权限并再次取消注释,以便令牌得到刷新。 但是,这样做没有任何区别,我仍然得到上面显示的错误。

是否有人可以协助我修复此错误,以便我们的志愿者可以再次查看他们的日历和日程安排?

非常感谢你们!

此页面的完整代码(当然省略CSS)如下所示。

<?php
/*
 * OAuth and Timetag connection stuff:
 */
use Joomla\Registry\Registry;

$options = new Registry([
"clientid" => "MY_CLIENT_ID",
"clientsecret" => "MY_CLIENT_SECRET",
"authurl" => "https://timetag.it/api_oauth_authorize",
"redirecturi" => "WEBSITE_URL",
"scope" => "general",
"tokenurl" => "https://timetag.it/oauth/token",
"userefresh" => true,
"sendheaders" => true
    ]);

$client = new JOAuth2Client($options);

// Manually reformat the token, because the format received from Timetag is different than the one accepted by JOAuth2Client
function reformatToken($token) {
$newToken = json_decode(key($token), true);
$newToken["created"] = $newToken["created_at"];
unset($newToken["created_at"]);

return $newToken;
}

$db = JFactory::getDbo();

$dbTokenConditions = array(
$db->quoteName("user_id") . " = " . $db->quote(1), // Custom id
$db->quoteName("profile_key") . " = " . $db->quote("profile.timetag_token")     // Custom key
);

// Get token from database
$query = $db->getQuery(true);
$query->select("*")->from($db->quoteName("#__user_profiles"))->where($dbTokenConditions);
$result = $db->setQuery($query)->loadObject();
$token = json_decode($result->profile_value, true);

// Only authenticate when there's no token available
if (!$token) {
$client->authenticate();
$client->setToken(reformatToken($client->getToken()));
} else {
   $client->setToken($token);
}

// Manually refresh to be able to reformat before use
if (array_key_exists("expires_in", $token) && $token["created"] +     $token["expires_in"] < time() + 20) {
$client->refreshToken($token["refresh_token"]);
    $client->setToken(reformatToken($client->getToken()));
}

// After authenticate/refresh, save valid token to database, only if it's a new token
if (!$token || $token["created"] < $client->getToken()["created"]) {
$query = $db->getQuery(true);
$fields = array($db->quoteName("profile_value") . " = " . $db->quote(json_encode($client->getToken())));
$query->update($db->quoteName("#__user_profiles"))->set($fields)->where($dbTokenConditions);
$db->setQuery($query)->execute();
}

/*
 * The script to get the engagement:
 */

// GET all the calendars from timetag.
$organizations = $client->query("https://timetag.it/api/v1.0.1/organizations");
$organizations = json_decode($organizations->body, true);

$name = str_replace(' ', '', JFactory::getUser()->username);
$format = "Y-m-d\TH:i:s"; // RFC3339
// First day of this month
$min_time = date($format, strtotime("first day of " . date("M")));
$max_time = date($format, strtotime("first day of next month " . date("M")));
// First day of this year
$min_time_year = date($format, strtotime("first day of January " . date("Y")));
$hoursSpendThisMonth = $hoursSpendThisYear = 0;

// Iterate over each calendar
foreach ($organizations as $organization => $datavalues) {

$organizationId = $datavalues["id"];
$projects = $client->query("https://timetag.it/api/v1.0.1/projects?organization_id=" . $organizationId);
$projects = json_decode($projects->body, true);

    // Iterate over each event that belongs to the logged in user (project)
    foreach ($projects as $project => $values) {
    if ($values["name"] === $name) {
        $projectId = $values["id"];
        $monthdata = $client->query("https://timetag.it/api/v1.0.1/projects/" . $projectId . "/hours_spent?organization_id=" . $organizationId . "&min_time=" . $min_time . "&max_time=" . $max_time);
        $hoursSpendThisMonth += json_decode($monthdata->body, true)["hours_spent"];

        $yeardata = $client->query("https://timetag.it/api/v1.0.1/projects/" . $projectId . "/hours_spent?organization_id=" . $organizationId . "&min_time=" . $min_time_year);
        $hoursSpendThisYear += json_decode($yeardata->body, true)["hours_spent"];
    }
}
}

// Convert base 100 to minutes(base 60).
$timeSpendThisMonth['hours'] = floor($hoursSpendThisMonth);
$timeSpendThisMonth['minutes'] = $hoursSpendThisMonth - $timeSpendThisMonth['hours'];
$timeSpendThisMonth['minutes'] = round($timeSpendThisMonth['minutes'] * 60);
$timeSpendThisYear['hours'] = floor($hoursSpendThisYear);
$timeSpendThisYear['minutes'] = $hoursSpendThisYear - $timeSpendThisYear['hours'];
$timeSpendThisYear['minutes'] = round($timeSpendThisYear['minutes'] * 60);

// Redirect if authentication code is still in the URL
if (isset($_GET["code"]))
JFactory::getApplication()->redirect(JURI::current());

// Get the hours value from JomSocial
$engagedMonthly = CFactory::getUser()->getInfo("FIELD_HOURS") ? : 192 / 12;
?>

<input id="firstTab" type="radio" name="tabs" checked>
<label for="firstTab">Maand</label>

<input id="secondTab" type="radio" name="tabs">
<label for="secondTab">Jaar</label>

<div class="content">
<div class="monthlyEngagement">
    <span class="workedHours"><? echo $timeSpendThisMonth['hours'] ?>u <?     echo $timeSpendThisMonth['minutes'] ?>m</span>/<? echo $engagedMonthly ?>u deze maand
    <br>
    <div class="progressBar">
        <div class="percentage" style="width:<? echo $hoursSpendThisMonth > $engagedMonthly ? 250 : $hoursSpendThisMonth/$engagedMonthly*250 ?>px"></div>
    </div>
</div>

<div class="yearlyEngagement">
    <span class="workedHours"><? echo $timeSpendThisYear['hours'] ?>u <? echo $timeSpendThisYear['minutes'] ?>m</span> gewerkt in <? echo date("Y") ?>
</div>
</div>

0 个答案:

没有答案