无法从我的站点访问 Google 驱动器 API
Can't get access from my site to Google Drive API
我有一个不允许超出我们网络的网站。我们使用一些域名从我们公司的网络访问它(比如 desire.it.loc
)。现在我需要从我的网站上传一些文件到 google 驱动器。到目前为止,我首先要做的是对 google 帐户进行身份验证。我在 google developers console
创建了项目,获得 client_id,秘密等。现在我得到
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 ' WebProperty: ' ,$wp['name'], " " , $wp['id'], "<br /> \n";
$views = $wp->getProfiles();
if (!is_null($views)) {
foreach($wp->getProfiles() as $view) {
// echo ' View: ' ,$view['name'], " " , $view['id'], "<br /> \n";
}
}
}
} // closes account summaries
}
print "<br><br><br>";
print "Access from google: " . $_SESSION['token'];
?>
我不是我们网络服务器的管理员,所以我想我很难更改那里的一些设置。我可以在不注册 "fake domains" 或类似的东西的情况下以某种方式修复它吗?
$client->setRedirectUri('http://x.x.x.x:x'); 是不允许的。您需要一个域名,例如 dev.example.com。您可以配置 /etc/hosts(或 windows 等效项)以将 dev.example.com 解析为 IP 地址。需要在GoogleAPI控制台注册相同的域名。
我有一个不允许超出我们网络的网站。我们使用一些域名从我们公司的网络访问它(比如 desire.it.loc
)。现在我需要从我的网站上传一些文件到 google 驱动器。到目前为止,我首先要做的是对 google 帐户进行身份验证。我在 google developers console
创建了项目,获得 client_id,秘密等。现在我得到
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 ' WebProperty: ' ,$wp['name'], " " , $wp['id'], "<br /> \n";
$views = $wp->getProfiles();
if (!is_null($views)) {
foreach($wp->getProfiles() as $view) {
// echo ' View: ' ,$view['name'], " " , $view['id'], "<br /> \n";
}
}
}
} // closes account summaries
}
print "<br><br><br>";
print "Access from google: " . $_SESSION['token'];
?>
我不是我们网络服务器的管理员,所以我想我很难更改那里的一些设置。我可以在不注册 "fake domains" 或类似的东西的情况下以某种方式修复它吗?
$client->setRedirectUri('http://x.x.x.x:x'); 是不允许的。您需要一个域名,例如 dev.example.com。您可以配置 /etc/hosts(或 windows 等效项)以将 dev.example.com 解析为 IP 地址。需要在GoogleAPI控制台注册相同的域名。