【问题标题】:Google OAuth 2.0 PHP API Authentication fails: "Use of undefined constant STDIN" [duplicate]Google OAuth 2.0 PHP API 身份验证失败:“使用未定义的常量 STDIN”[重复]
【发布时间】:2018-07-19 18:59:22
【问题描述】:

我正在尝试实现PlaylistItems: insertYouTube Data API sample,当我尝试运行它时,我得到以下信息:

注意:使用未定义的常量 STDIN - 在第 51 行的 /example/path/to/script.php 中假定为“STDIN”

包含这一行的函数并没有明确说明 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;
}

请原谅我的幼稚,但脚本应该如何提供身份验证代码?为什么不执行这一步?

【问题讨论】:

    标签: php google-oauth inputstream stdin


    【解决方案1】:

    感谢@sean-bright 指出现有问题。虽然它没有回答我的问题,但它让我更接近解决方案。

    事实证明,Google 的示例设计为从 CLI 运行,而我基于浏览器的实现没有提供提示用户输入 STDIN 行所要求的输入的方法。

    由于身份验证代码作为$authUrl 变量中的参数提供,该变量在脚本第一次运行时打印到屏幕上,因此我能够通过替换解决我的问题

    $authCode = trim(fgets(STDIN));

    与:

    $authCode = $_GET['code'];

    【讨论】:

      猜你喜欢
      • 2012-11-27
      • 1970-01-01
      • 2011-11-16
      • 2014-05-03
      • 2014-12-24
      • 2021-01-16
      • 2015-03-13
      • 2012-05-29
      • 2015-02-25
      相关资源
      最近更新 更多