Google OAuth 2.0 PHP API 身份验证失败:"Use of undefined constant STDIN"
Google OAuth 2.0 PHP API Authentication fails: "Use of undefined constant STDIN"
我正在尝试实现 PlaylistItems: insert
YouTube Data API sample,当我尝试 运行 它时,我得到以下信息:
通知:使用未定义常量 STDIN - 假设 'STDIN' in /example/path/to/script.php 线上 51
包含这一行的函数没有说明 STDIN 从哪里获取它的值:
function getClient() {
$client = new Google_Client();
$client->setApplicationName('API Samples');
$client->setScopes('https://www.googleapis.com/auth/youtube.force-ssl');
// Set to name/location of your client_secrets.json file.
$client->setAuthConfig('client_secrets.json');
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} 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)); // <--- Appears to be the problem
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
// Store the credentials to disk.
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
return $client;
}
请原谅我的天真,但脚本应该如何提供授权码?为什么不执行此步骤?
感谢@sean-bright 指出现有问题。虽然它没有回答我的问题,但它让我更接近解决方案。
事实证明 Google 的样本被设计为来自 CLI 的 运行,而我基于浏览器的实现没有提供提示用户输入的方法STDIN
行。
由于授权码是作为 $authUrl
变量中的参数提供的,该变量在脚本第一次 运行 时被打印到屏幕上,我能够通过替换 运行 来解决我的问题=14=]
$authCode = trim(fgets(STDIN));
与:
$authCode = $_GET['code'];
我正在尝试实现 PlaylistItems: insert
YouTube Data API sample,当我尝试 运行 它时,我得到以下信息:
通知:使用未定义常量 STDIN - 假设 'STDIN' in /example/path/to/script.php 线上 51
包含这一行的函数没有说明 STDIN 从哪里获取它的值:
function getClient() {
$client = new Google_Client();
$client->setApplicationName('API Samples');
$client->setScopes('https://www.googleapis.com/auth/youtube.force-ssl');
// Set to name/location of your client_secrets.json file.
$client->setAuthConfig('client_secrets.json');
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} 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)); // <--- Appears to be the problem
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
// Store the credentials to disk.
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
return $client;
}
请原谅我的天真,但脚本应该如何提供授权码?为什么不执行此步骤?
感谢@sean-bright 指出现有问题。虽然它没有回答我的问题,但它让我更接近解决方案。
事实证明 Google 的样本被设计为来自 CLI 的 运行,而我基于浏览器的实现没有提供提示用户输入的方法STDIN
行。
由于授权码是作为 $authUrl
变量中的参数提供的,该变量在脚本第一次 运行 时被打印到屏幕上,我能够通过替换 运行 来解决我的问题=14=]
$authCode = trim(fgets(STDIN));
与:
$authCode = $_GET['code'];