代码之家  ›  专栏  ›  技术社区  ›  Keyslinger

google oauth 2.0 php api身份验证失败:“使用未定义的常量stdin”[重复]

  •  0
  • Keyslinger  · 技术社区  · 7 年前

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

    通知 :使用未定义的常量stdin-在中假定为“stdin” /示例/path/to/script.php 在线 五十一

    包含此行的函数不清楚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;
    }
    

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

    1 回复  |  直到 7 年前
        1
  •  0
  •   Keyslinger    7 年前

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

    事实证明,Google的示例设计为从一个cli运行,而我基于浏览器的实现并没有提供一种方法来提示用户输入该命令所要求的输入。 STDIN 线。

    因为在 $authUrl 第一次运行脚本时打印到屏幕上的变量,我可以通过替换

    $authCode = trim(fgets(STDIN));

    用:

    $authCode = $_GET['code'];