![](/img/trans.png)
[英]How to authenticate with Google Service Account credentials using Postman and HTTPS?
[英]Uncaught InvalidArgumentException: missing the required redirect URI when using Google Account Service (json credentials) to authenticate
<?php
require __DIR__ . '/vendor/autoload.php';
//create a Google OAuth client
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('People API PHP Quickstart');
$client->setScopes(Google_Service_PeopleService::CONTACTS_READONLY);
$client->setAuthConfig('secret.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 {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$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;
}
$client = getClient();
嗯,所以當我跑步時,我得到:
PHP Fatal error: Uncaught InvalidArgumentException: missing the required redire ct URI in C:\Sources\scripts\script-02\vendor\google\auth\src\OAuth2.php:684 Stack trace:
#0 C:\Sources\scripts\script-02\vendor\google\apiclient\src\Client.php(396): Goo gle\Auth\OAuth2->buildFullAuthorizationUri(Array)
#1 C:\Sources\scripts\script-02\upload.php(32): Google\Client->createAuthUrl()
#2 C:\Sources\scripts\script-02\upload.php(55): getClient()
#3 {main} thrown in C:\Sources\scripts\script-02\vendor\google\auth\src\OAuth2.php on li ne 684
Fatal error: Uncaught InvalidArgumentException: missing the required redirect UR I in C:\Sources\scripts\script-02\vendor\google\auth\src\OAuth2.php:684 Stack trace:
#0 C:\Sources\scripts\script-02\vendor\google\apiclient\src\Client.php(396): Goo gle\Auth\OAuth2->buildFullAuthorizationUri(Array)
#1 C:\Sources\scripts\script-02\upload.php(32): Google\Client->createAuthUrl()
#2 C:\Sources\scripts\script-02\upload.php(55): getClient()
#3 {main} thrown in C:\Sources\scripts\script-02\vendor\google\auth\src\OAuth2.php on li ne 684
我下載了憑據並將其放入 secret.json 中,我不確定自己做錯了什么。 不需要任何重定向 URL。 我在我的帳戶上設置了一個 OAuth2 憑據,但當我意識到它不是用於服務器到服務器連接或允許我們通過簡單腳本將文件上傳到 Google Drive 時刪除了它。
https://developers.google.com/docs/api/quickstart/php
有趣的是,我正在使用該指南,並按照他們的文檔告訴我做的一切,但它根本不起作用。
我試過:
urn:ietf:wg:oauth:2.0:oob
但同樣它不起作用,它只能在本機應用程序中使用。
我認為圖書館本身或 Google 有問題。 此外,每當我嘗試使用它時,我經常會得到:
在瀏覽器中打開以下鏈接:
並且該鏈接不允許我允許訪問。
我不知道他們希望人們如何使用他們的 API。
訪問https://console.cloud.google.com/apis/credentials
單擊您正在使用的 OAuth 2 客戶端 ID。
在授權重定向 URI 下
點擊添加URI
添加將 uri 添加到此文件
http://localhost/quickstart.php
secret.json
到secret.json
添加以下行:
$redirect_uri = 'http://'。 $_SERVER['HTTP_HOST']. $_SERVER['PHP_SELF']; $client->setRedirectUri($redirect_uri);
參考: https://github.com/googleapis/google-api-php-client#authentication-with-oauth
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.