如何在调用youtube commentthread api时设置$ _GET ['代码']和$ _SESSION ['令牌']

时间:2016-09-06 06:37:14

标签: php api session youtube-api comments

我使用youtube api的完整代码插入commentthread youtube_v3但是当我调用api时,它会给我以下错误。

Authorization Required

You need to authorize access before proceeding.

当我点击授权访问时,它会重新启动谷歌错误页面并显示在内容

下面
400. That’s an error.

Error: redirect_uri_mismatch

Application: Application Name

You can email the developer of this application at: email address

The redirect URI in the request, http://localhost/done.php, does not match the ones authorized for the OAuth client. Visit https://console.developers.google.com/apis/credentials/oauthclient/******-*******.apps.googleusercontent.com?project=******* to update the authorized redirect URIs.

在此代码中,$_GET['code']$_SESSION['token']未设置,因此我无法获取访问令牌,因此它会给我redirect_uri_mismatch错误。

设置$_GET['code']$_SESSION['token']的值。 代码:

if (isset($_GET['code'])) {
  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    die('The session state did not match.');
  }

  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: ' . $redirect);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

如果你有

给我解决方案

先谢谢。

1 个答案:

答案 0 :(得分:0)

根据此thread,返回响应的重定向URI必须在API控制台中注册,而error表示您尚未执行此操作,或者没有正确完成它。

  

转到项目的控制台,查看API Access。您应该会看到您的客户ID和那里的秘密,以及重定向URI列表。如果未列出您想要的URI,请单击编辑设置并将URI添加到列表中。

您还可以查看这些相关的SO问题:

希望这有帮助!