Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 谷歌+;使用电子邮件获取用户ID?_Php_Oauth 2.0_Google Plus - Fatal编程技术网

Php 谷歌+;使用电子邮件获取用户ID?

Php 谷歌+;使用电子邮件获取用户ID?,php,oauth-2.0,google-plus,Php,Oauth 2.0,Google Plus,我想知道是否可以使用Google+api获取“userId”值,以便显示指向某个特定用户配置文件的链接。为了更好地解释,我有一个PHP应用程序,还有一个面板,我可以在其中查看数据库中的现有用户。因此,当详细介绍某个特定用户时,我想显示一个Google+徽章,该徽章将链接到该用户的Google+个人资料。我没有“用户ID”,但仍然可以访问他的电子邮件地址。有什么建议吗?没有端点可以用来将电子邮件地址转换为Google+用户ID 但是,您可以使用RESTAPI确定当前登录用户的Google+用户ID

我想知道是否可以使用Google+api获取“userId”值,以便显示指向某个特定用户配置文件的链接。为了更好地解释,我有一个PHP应用程序,还有一个面板,我可以在其中查看数据库中的现有用户。因此,当详细介绍某个特定用户时,我想显示一个Google+徽章,该徽章将链接到该用户的Google+个人资料。我没有“用户ID”,但仍然可以访问他的电子邮件地址。有什么建议吗?

没有端点可以用来将电子邮件地址转换为Google+用户ID

但是,您可以使用RESTAPI确定当前登录用户的Google+用户ID。这需要用户通过OAuth授予您在Google+上访问其身份的权限。要做到这一点,请将端点与我一起用作用户ID。您可以在上进行尝试,以观察OAuth对话框的用户体验

下面是从中提取的一些代码,说明了获取当前用户的userId所需的一切:

if (ini_get('register_globals') === "1") {
 die("register_globals must be turned off before using the starter application");
}

require_once 'google-api-php-client/src/apiClient.php';
require_once 'google-api-php-client/src/contrib/apiPlusService.php';

session_start();

$client = new apiClient();
$client->setApplicationName("Google+ PHP Starter Application");
// Visit https://code.google.com/apis/console to generate your
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.
$client->setClientId('insert_your_oauth2_client_id');
$client->setClientSecret('insert_your_oauth2_client_secret');
$client->setRedirectUri('insert_your_oauth2_redirect_uri');
$client->setDeveloperKey('insert_your_developer_key');
$client->setScopes(array('https://www.googleapis.com/auth/plus.me'));
$plus = new apiPlusService($client);

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}

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

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

if ($client->getAccessToken()) {
  $me = $plus->people->get('me');
  // Do stuff with the id
  echo $me['id'];

  // The access token may have been updated lazily.
  $_SESSION['access_token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
}

没有可以点击的端点将电子邮件地址转换为Google+用户ID

但是,您可以使用RESTAPI确定当前登录用户的Google+用户ID。这需要用户通过OAuth授予您在Google+上访问其身份的权限。要做到这一点,请将端点与我一起用作用户ID。您可以在上进行尝试,以观察OAuth对话框的用户体验

下面是从中提取的一些代码,说明了获取当前用户的userId所需的一切:

if (ini_get('register_globals') === "1") {
 die("register_globals must be turned off before using the starter application");
}

require_once 'google-api-php-client/src/apiClient.php';
require_once 'google-api-php-client/src/contrib/apiPlusService.php';

session_start();

$client = new apiClient();
$client->setApplicationName("Google+ PHP Starter Application");
// Visit https://code.google.com/apis/console to generate your
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.
$client->setClientId('insert_your_oauth2_client_id');
$client->setClientSecret('insert_your_oauth2_client_secret');
$client->setRedirectUri('insert_your_oauth2_redirect_uri');
$client->setDeveloperKey('insert_your_developer_key');
$client->setScopes(array('https://www.googleapis.com/auth/plus.me'));
$plus = new apiPlusService($client);

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}

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

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

if ($client->getAccessToken()) {
  $me = $plus->people->get('me');
  // Do stuff with the id
  echo $me['id'];

  // The access token may have been updated lazily.
  $_SESSION['access_token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
}

非常感谢您的回答!非常有用!再次感谢,这里有一个将电子邮件地址转换为Google+ID的端点:
http://picasaweb.google.com/data/entry/api/user/**替换为电子邮件地址**?alt=json
。它没有很好的文档记录,所以我不会在任何专业上依赖它。非常感谢你的回答!非常有用!再次感谢,这里有一个将电子邮件地址转换为Google+ID的端点:
http://picasaweb.google.com/data/entry/api/user/**替换为电子邮件地址**?alt=json
。它没有很好的文档记录,所以我不会在任何重要的事情上依赖它。