返回
更改 Google 表格 OAuth 范围后追加行无效凭据错误解决方法
php
2024-03-17 09:16:44
## 授予无效凭据错误:在更改 Google 表格 OAuth 范围后追加行
问题
更改 Google 表格 OAuth 范围后,在追加行或标题时会遇到以下错误:
Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential.
解决方法
解决此问题的步骤如下:
1. 检查范围权限
确保已更新 Google 开发者控制台和本地配置中的范围权限为 "https://www.googleapis.com/auth/userinfo.email"
。
2. 验证凭据有效性
- 确认访问令牌和刷新令牌仍然有效。
- 使用刷新令牌更新访问令牌(如果已过期)。
- 检查凭据是否已被撤销或过期。
3. 确保用户授权
对于现有用户,验证他们是否已授权应用程序访问其 Google 帐户并授予必要的权限。
4. 检查服务端实现
仔细审查客户端初始化、用户授权和行追加过程中的服务端实现。
5. 尝试重新授权
如果问题仍然存在,请重新授权用户以重置授权并解决凭据或授权状态问题。
6. 联系 Google 支持
如果以上方法无效,请考虑联系 Google 支持团队以获得进一步的帮助。
代码优化
授权服务:
public function authorizeService(Request $request)
{
try {
// ... Existing code
$client->setState(base64_encode(json_encode([
'form_name' => $request->get('form_name'),
'form_id' => $request->get('form_id'),
'service' => $request->get('service'),
'scope' => $client->getScopes()
])));
// ... Existing code
} catch (Exception $e) {
\Log::emergency("File:" . $e->getFile(). "Line:" . $e->getLine(). "Message:" . $e->getMessage());
return $this->respondWentWrong($e);
}
}
初始化客户端:
public function initializeClient($integration)
{
try {
// ... Existing code
if ($this->client->isAccessTokenExpired()) {
$this->client->setAccessToken($refresh_token);
if (!$this->client->fetchAccessTokenWithRefreshToken($refresh_token)) {
// Handle the case where the refresh token is invalid or expired
}
$accessTokenUpdated = $this->client->getAccessToken();
$this->client->setAccessToken($accessTokenUpdated);
// ... Existing code
}
} catch (Exception $e) {
\Log::emergency("File:" . $e->getFile(). "Line:" . $e->getLine(). "Message:" . $e->getMessage());
return $this->respondWentWrong($e);
}
}
追加行:
public function appendRowsOnSpreadsheet($integration, $submission)
{
try {
$this->initializeClient($integration);
// ... Existing code
/*
* Make the API request to append data
*/
try {
$response = $guzzleClient->post($url, [
'headers' => $headers,
'json' => $requestBody,
'query' => [
'valueInputOption' => 'RAW'
],
]);
$responseData = json_decode($response->getBody(), true);
$output = [
'success' => true,
'msg' => __('form.success'),
'response' => $responseData
];
} catch (GuzzleException $e) {
if ($e->getResponse() && $e->getResponse()->getBody()) {
$error = json_decode($e->getResponse()->getBody(true), true);
if (isset($error['error']['code']) && $error['error']['code'] == 401) {
// Handle the case where the access token is invalid or expired
}
}
$output = [
'success' => false,
'msg' => $e->getMessage()
];
}
} catch (Exception $e) {
\Log::emergency("File:" . $e->getFile(). "Line:" . $e->getLine(). "Message:" . $e->getMessage());
$output = [
'success' => false,
'msg' => $e->getMessage()
];
}
return $output;
}
结论
遵循这些步骤可以解决更改 Google 表格 OAuth 范围后在追加行时遇到的无效凭据错误。确保仔细检查凭据、授权和服务端实现,以便顺利追加数据。
常见问题解答
1. 为什么会在更改范围后遇到此错误?
范围更改表明需要新的权限。由于用户尚未授权应用程序访问新的权限,因此会出现无效凭据错误。
2. 如何重新授权用户?
重新授权用户时,应用程序会将用户重定向到授权页面,在那里他们可以授予应用程序必要的权限。
3. 导致此错误的潜在原因是什么?
- 无效或过期的访问或刷新令牌
- 已撤销或过期的凭据
- 用户尚未授权应用程序访问其 Google 帐户
- 服务端实现中的授权错误
4. 如何避免此错误?
- 保持访问和刷新令牌的有效性
- 定期检查凭据状态
- 验证用户已授权应用程序访问其 Google 帐户
- 仔细审查服务端实现以确保正确的授权
5. 如果仍然收到此错误怎么办?
如果尝试了所有这些方法但仍然遇到此错误,请考虑联系 Google 支持团队以获得进一步的帮助。