当前位置: 首页 > 知识库问答 >
问题:

未捕获的InvalidArgumentException:向Google Calendar进行身份验证时json令牌无效

万浩淼
2023-03-14

我得到的错误是:PHP Fatal error:uncatted invalidargumentexception:C:\wamp64\www\marryme\vendor\google\apiclient\src\google\client.PHP:420 Stack trace:#0C:\wamp64\www\marryme\insertevent.PHP(26):google_client->setaccesstoken(NULL)#1C:\wamp64\www\marryme\vendor\google\apiclient\src\google\client.PHP中抛出的{main}

下面是我的代码:

require_once('./conection/init.php');
require __DIR__ . '/vendor/autoload.php';


/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function getClient()
{
  $client = new Google_Client();
  $client->setApplicationName('MarryMe');
  $client->setScopes(Google_Service_Calendar::CALENDAR_READONLY);
  $client->setAuthConfig('credentials.json');
  $client->setAccessType('offline');
  $client->setPrompt('select_account consent');

  // Load previously authorized token from a file, if it exists.
  // The file token.json stores the user's access and refresh tokens, and is
  // created automatically when the authorization flow completes for the first
  // time.
  $tokenPath = 'token.json';
  if (file_exists($tokenPath)) {
    $accessToken = json_decode(file_get_contents($tokenPath), true);
    $client->setAccessToken($accessToken);
  }

  // If there is no previous token or it's expired.
  if ($client->isAccessTokenExpired()) {
    // Refresh the token if possible, else fetch a new one.
    if ($client->getRefreshToken()) {
      $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    } else {
      if (!isset($_GET['code'])) {
        // Request authorization from the user.
        $authUrl = $client->createAuthUrl();
        header("location: $authUrl");
      } else {
        $code = $_GET['code'];
        $authCode = trim($code);
        var_dump($authCode);
        // Exchange authorization code for an access token.
        $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
        var_dump($accessToken);
        $client->setAccessToken($accessToken);
        // Check to see if there was an error.
        if (array_key_exists('error', $accessToken)) {
          throw new Exception(join(', ', $accessToken));
        }
      }
    }
    // Save the token to a file.
    if (!file_exists(dirname($tokenPath))) {
      mkdir(dirname($tokenPath), 0700, true);
    }
    file_put_contents($tokenPath, json_encode($client->getAccessToken()));
  }
  return $client;
}



// Get the API client and construct the service object.

$client = getClient();

$service = new Google_Service_Calendar($client);

// Print the next 10 events on the user's calendar.
$calendarId = 'primary';
$optParams = array(
  'maxResults' => 50,
  'orderBy' => 'startTime',
  'singleEvents' => true,
  'timeMin' => date('c'),
);
$results = $service->events->listEvents($calendarId, $optParams);
$events = $results->getItems();


$event = new Google_Service_Calendar_Event(array(
  'summary' =>  $_SESSION['name'],
  'location' => ' ',
  'description' => $_SESSION['description'],
  'start' => array(
    'dateTime' => $_SESSION['start_date'],
    'timeZone' => 'UTC',
  ),
  'end' => array(
    'dateTime' => $_SESSION['due_date'],
    'timeZone' => 'UTC',
  ),

  'reminders' => array(
    'useDefault' => FALSE,
    'overrides' => array(
      array('method' => 'email', 'minutes' => 24 * 60),
      array('method' => 'popup', 'minutes' => 10),
    ),
  ),
));

$calendarId = 'primary';
$event = $service->events->insert($calendarId, $event);

header("location:./includes/tasksProcess/tasks.php");


//end of insert event ```


共有1个答案

梁丘德寿
2023-03-14

确保__dir__.位于实例化凭据的位置之前,以便函数知道在哪里查找json文件

所以应该是这样的

$client->setauthconfig(__dir__.'credentials.json');

 类似资料:
  • 致命:“https://github.com/scuzzlebuzzle/ol3-1.git/'”身份验证失败

  • 我有一个LaravelAPI(实际上是LumenAPI)服务于VueJS前端。Vue应用程序允许用户登录到谷歌。然后将Google令牌发送回Lumen API,后者使用Google验证令牌,然后验证电子邮件地址是否为有效用户。然后它生成一个令牌,与用户一起存储在数据库中,并返回用户对象。 我没有使用Passport或jwt auth之类的东西。那么现在,我如何使用默认的Auth中间件来验证(现在已

  • 我询问了如何建立一个服务呼叫,并在HttpClient上获得了一个很好的信息。然而,虽然这个问题在技术上得到了回答,但我还是被卡住了。 在控制台中,我可以看到我的浏览器向服务发送了什么请求来获取授权令牌。然而,当我尝试在我的服务层中模拟构建请求的调用时,我得到以下错误消息。我在这里犯错的可能性很大。不知道该用谷歌搜索什么,真的。。。 "StatusCode: 500, ReasonPhrase:'

  • 我的管理员控制器扩展了控制器类,在那里我使用方法加载视图页面并传递保护。config/auth.php也通过添加管理员保护和提供程序进行了修改。在app/Actions/Fortify文件夹中,我添加了AttemptToAuthenticate和ReDirectIfTwoFactorAuthenticate类,并更改了命名空间。我的管理员控制器还扩展了Guard。用户的重定向中间件和管理员的重定向

  • 我刚刚开始在.NET中开发我的第一个REST API。由于它将是无状态的,我将使用令牌进行身份验证: 基本思想(System.Security.Cryptography): null 检查凭据是否有效(用户名,将哈希密码与db值进行比较) 如果为真,则加密数据对象 对生成的令牌使用HMAC,并将其存储到数据库 将令牌(不带HMAC)返回给用户(cookie/字符串) 对需要身份验证的方法的请求: