如果断开连接,您需要重置应用的状态以刷新$tocken。
关于处理 API 错误的 Google API office 文档
401: Invalid Credentials
授权标头无效。您使用的访问令牌是
过期或无效。
{ "error": {
> "errors": [
> {
> "domain": "global",
> "reason": "authError",
> "message": "Invalid Credentials",
> "locationType": "header",
> "location": "Authorization",
> }
> ],
> "code": 401,
> "message": "Invalid Credentials" } }
建议的操作:使用长寿命的刷新访问令牌
刷新令牌。如果失败,引导用户通过 OAuth 流程,
如授权您的应用中所述
在 singin.php 的第 1 行也清楚地注释了它。 98:
// Normally the state would be a one-time use token, however in our
// simple case, we want a user to be able to connect and disconnect
// without reloading the page. Thus, for demonstration, we don't
// implement this best practice.
//$app['session']->set('state', '');
因此,在您的情况下,您的应用似乎已断开连接,从而导致 $token 变为空。因此在第 91 行强制执行此代码块
if (empty($token)) {
// Ensure that this is no request forgery going on, and that the user
// sending us this connect request is the user that was supposed to.
if ($request->get('state') != ($app['session']->get('state'))) {
return new Response('Invalid state parameter', 401);
}