【问题标题】:Can't get access from my site to Google Drive API无法从我的网站访问 Google Drive API
【发布时间】:2015-06-15 18:52:52
【问题描述】:

我有一个网站不允许进入我们的网络。我们使用一些域名从我们公司的网络访问它(比如desire.it.loc)。现在我需要从我的网站上传一些文件到谷歌驱动器。到目前为止,我首先应该做的是对谷歌帐户进行身份验证。我在google developers console 创建了项目,得到了client_id、secret 等。现在我得到了

Error: invalid_request
device_id and device_name are required for private IP: http://x.x.x.x:x

我的php代码:

session_start();
$client = new Google_Client();
$client->setApplicationName("limits");
$client->setDeveloperKey("<key goes here>");    
$client->setClientId('<id goes here>');
$client->setClientSecret('<secret goes here>');    
$client->setRedirectUri('http://x.x.x.x:x');
$client->setScopes(array('http://x.x.x.x:x'));    

if ($_GET['logout'] == "1") {
    unset($_SESSION['token']);
}    

if (isset($_GET['code'])) {    
    $client->authenticate($_GET['code']);
    $_SESSION['token'] = $client->getAccessToken();
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}    

if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
    $authUrl = $client->createAuthUrl(); // error goes here
    print "<a class='login' href='$authUrl'>Connect Me!</a>";
}    

if (isset($_SESSION['token'])) {
    print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>";
    $client->setAccessToken($_SESSION['token']);
    $service = new Google_Service_Analytics($client);    

    $accounts = $service->management_accountSummaries->listManagementAccountSummaries();

    foreach ($accounts->getItems() as $item) {
        echo "Account: ",$item['name'], "  " , $item['id'], "<br /> \n";
        foreach($item->getWebProperties() as $wp) {
            echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebProperty: ' ,$wp['name'], "  " , $wp['id'], "<br /> \n";

            $views = $wp->getProfiles();
            if (!is_null($views)) {
                foreach($wp->getProfiles() as $view) {
                    //  echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;View: ' ,$view['name'], "  " , $view['id'], "<br /> \n";
                }
            }
        }
    } // closes account summaries

}
print "<br><br><br>";
print "Access from google: " . $_SESSION['token'];
?>

我不是我们网络服务器的管理员,所以我想我很难在那里更改一些设置。我可以在不注册“假域名”或类似的东西的情况下以某种方式修复它吗?

【问题讨论】:

  • 您在做什么会引发该错误消息?
  • @pinoyyid 将我的代码添加到问题中
  • 哪一行导致错误?
  • @pinoyyid $authUrl = $client-&gt;createAuthUrl();

标签: php oauth-2.0 google-drive-api


【解决方案1】:

$client->setRedirectUri('http://x.x.x.x:x'); 是不允许的。您需要一个域名,例如 dev.example.com。您可以配置您的 /etc/hosts(或 Windows 等效项)以将 dev.example.com 解析为 IP 地址。同一个域名需要在 Google API Console 中注册。

【讨论】:

  • 我试过了Invalid parameter value for redirect_uri: Non-public domains not allowed: http://limits.desire.it.loc/
  • 它不喜欢 .loc。试试“foo.bar.com”
  • 所以应该添加网络服务器设置?就我而言,它是 nginx
  • 除非您的 nginx 配置为拒绝未配置的 URL,否则您不需要触摸它。只需将 foo.bar.com 的映射添加到本地 /etc/hosts 中的 IP 地址即可。如果您需要为许多 PC 使用此功能,则需要在某处设置 DNS 记录,可以在内部 DNS 服务器上,也可以在公共服务器上,具体取决于您的网络配置。这应该是 stackexchange 其他地方的一个新问题,标记为 dns、nginx、主机
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-13
  • 2017-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多