Google OAuth 2.0 PHP API身份验证失败:“使用未定义的常量STDIN" [英] Google OAuth 2.0 PHP API Authentication fails: "Use of undefined constant STDIN"

查看:59
本文介绍了Google OAuth 2.0 PHP API身份验证失败:“使用未定义的常量STDIN"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现 PlaylistItems:插入 YouTube Data API示例,当我尝试运行该示例时,我得到以下信息:

通知:使用未定义的常量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');//设置为client_secrets.json文件的名称/位置.$ client-> setAuthConfig('client_secrets.json');$ client-> setAccessType('offline');//从文件加载先前授权的凭据.$ credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);如果(file_exists($ credentialsPath)){$ accessToken = json_decode(file_get_contents($ credentialsPath),true);} 别的 {//向用户请求授权.$ authUrl = $ client-> createAuthUrl();printf(在浏览器中打开以下链接:\ n%s \ n",$ authUrl);打印输入验证码:";$ authCode = trim(fgets(STDIN));//< ---似乎是问题所在//交换访问令牌的授权代码.$ accessToken = $ client-> fetchAccessTokenWithAuthCode($ authCode);//将凭据存储到磁盘.if(!file_exists(dirname($ credentialsPath))){mkdir(dirname($ credentialsPath),0700,true);}file_put_contents($ credentialsPath,json_encode($ accessToken));printf(凭据保存到%s \ n",$ credentialsPath);}$ client-> setAccessToken($ accessToken);//刷新令牌(如果令牌已过期).如果($ client-> isAccessTokenExpired()){$ client-> fetchAccessTokenWithRefreshToken($ client-> getRefreshToken());file_put_contents($ credentialsPath,json_encode($ client-> getAccessToken()));}返回$ client;} 

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

解决方案

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

事实证明Google的示例旨在从CLI运行,而我基于浏览器的实现并未提供提示用户输入该 STDIN 行所要求的输入的方法./p>

由于auth代码是作为 $ authUrl 变量中的参数提供的,该变量在首次运行脚本时会显示在屏幕上,因此我能够通过替换来解决问题

$ authCode = trim(fgets(STDIN));

具有:

$ authCode = $ _GET ['code'];

I am trying to implement the PlaylistItems: insert YouTube Data API sample, and when I try to run it, I get the following:

Notice: Use of undefined constant STDIN - assumed 'STDIN' in /example/path/to/script.php on line 51

The function containing this line does not make it clear where STDIN gets it's value:

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;
}

Please forgive my naiveté, but how is the script supposed to supply an auth code? Why is this step not performed?

解决方案

Thanks, @sean-bright for pointing out the existing question. While it didn't not answer my question, it got me closer to the solution.

It turns out Google's sample is designed to be run from a CLI and my browser-based implementation was not providing a means of prompting the user for input that is called for by that STDIN line.

Since the auth code is provided as a parameter in the $authUrl variable that gets printed to the screen the first time the script is run, I was able to resolve my issue by replacing

$authCode = trim(fgets(STDIN));

with:

$authCode = $_GET['code'];

这篇关于Google OAuth 2.0 PHP API身份验证失败:“使用未定义的常量STDIN"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆