Google api 如何为$plus指定字段->;人民->;打电话给我?
对于库,我使用了以下代码,这些代码运行良好,并打印了大量关于用户的信息,用户通过OAuth2授权我的应用程序:Google api 如何为$plus指定字段->;人民->;打电话给我?,google-api,google-plus,google-api-php-client,google-api-client,google-oauth,Google Api,Google Plus,Google Api Php Client,Google Api Client,Google Oauth,对于库,我使用了以下代码,这些代码运行良好,并打印了大量关于用户的信息,用户通过OAuth2授权我的应用程序: <?php require_once('google-api-php-client-1.1.7/src/Google/autoload.php'); const TITLE = 'My amazing app'; const REDIRECT = 'https://example.com/myapp/'; session_start(); $client = new Go
<?php
require_once('google-api-php-client-1.1.7/src/Google/autoload.php');
const TITLE = 'My amazing app';
const REDIRECT = 'https://example.com/myapp/';
session_start();
$client = new Google_Client();
$client->setApplicationName(TITLE);
$client->setClientId('REPLACE_ME.apps.googleusercontent.com');
$client->setClientSecret('REPLACE_ME');
$client->setRedirectUri(REDIRECT);
$client->setScopes(array(Google_Service_Plus::PLUS_ME));
$plus = new Google_Service_Plus($client);
if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
}
if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
error_log('The session state did not match.');
exit(1);
}
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
header('Location: ' . REDIRECT);
}
if (isset($_SESSION['access_token'])) {
$client->setAccessToken($_SESSION['access_token']);
}
if ($client->getAccessToken() && !$client->isAccessTokenExpired()) {
try {
$me = $plus->people->get('me'); # HOW TO SPECIFY FIELDS?
$body = '<PRE>' . print_r($me, TRUE) . '</PRE>';
} catch (Google_Exception $e) {
error_log($e);
$body = htmlspecialchars($e->getMessage());
}
# the access token may have been updated lazily
$_SESSION['access_token'] = $client->getAccessToken();
} else {
$state = mt_rand();
$client->setState($state);
$_SESSION['state'] = $state;
$body = sprintf('<P><A HREF="%s">Login</A></P>',
$client->createAuthUrl());
}
?>
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE><?= TITLE ?></TITLE>
</HEAD>
<BODY>
<?= $body ?>
<P><A HREF="<?= REDIRECT ?>?logout">Logout</A></P>
</BODY>
</HTML>
这同样可以很好地工作,并且只打印指定的字段:
我的问题:
如何指定上述$me=$plus->people->get('me')中的字段代码>呼叫
学习代码后:
/**
* Get a person's profile. If your app uses scope
* https://www.googleapis.com/auth/plus.login, this method is
* guaranteed to return ageRange and language. (people.get)
*
* @param string $userId The ID of the person to get the profile for. The
* special value "me" can be used to indicate the authenticated user.
* @param array $optParams Optional parameters.
* @return Google_Service_Plus_Person
*/
public function get($userId, $optParams = array())
{
$params = array('userId' => $userId);
$params = array_merge($params, $optParams);
return $this->call('get', array($params), "Google_Service_Plus_Person");
}
我尝试了以下PHP代码:
const FIELDS = 'id,gender,name,image,placesLived';
$me = $plus->people->get('me', array('fields' => urlencode(FIELDS)));
但出于某种原因,它打印了大量的:受保护的字符串:
Google_Service_Plus_Person Object
(
[collection_key:protected] => urls
[internal_gapi_mappings:protected] => Array
(
)
[aboutMe] =>
[ageRangeType:protected] => Google_Service_Plus_PersonAgeRange
[ageRangeDataType:protected] =>
[birthday] =>
[braggingRights] =>
[circledByCount] =>
[coverType:protected] => Google_Service_Plus_PersonCover
[coverDataType:protected] =>
[currentLocation] =>
[displayName] =>
[domain] =>
[emailsType:protected] => Google_Service_Plus_PersonEmails
[emailsDataType:protected] => array
[etag] =>
[gender] => male
...
此外,我还尝试在me
之后添加字段:
$me = $plus->people->get('me?fields=' . urlencode(FIELDS)));
但是得到404错误:
调用GET时出错
:
(404)未找到
更新:我在GitHUb创建。要指定从G+API获取哪些字段,只需在选项数组中指定一个字段成员。所以实际上你已经非常接近解决方案了:
$me = $plus->people->get('me', array('fields' => 'id,gender,name,image,placesLived'));
您甚至不必urlencode
,因为它是库本身的默认安全功能
可能欺骗您的是,Google\u Service\u Plus\u Person
类包含受保护成员的所有可能字段,而不考虑API发送的实际字段。对象中未包含的字段将为空。与往常一样,类的用户不应以任何方式使用受保护的成员
作为库的用户,您应该只使用公共成员,例如$me->getplacesLive()
和$me->getId()
。在开发过程中转储整个对象是一个不错的工具,但在生产过程中调用公共接口是一个不错的方法。谢谢,但是如何获取例如givename
?我尝试了($me->getName())['givenName']
,但出现语法错误。($me->getPlacesLived())[0]['value']
也未编译。这将是$me->getName()->getGivenName()
。PlacesLived是$me->getPlacesLived()->getPrimary()
或$me->getPlacesLived()->getValue()
来获取数组。$me->getName()->getGivenName()
工作正常,但不幸的是,我得到:PHP致命错误:调用成员函数getPrimary()在非对象上
和PHP致命错误:调用非对象上的成员函数getValue(),以获取其他2个建议。也许是因为它是一个数组的地方?对,图书馆是有点缺乏文档<代码>$me->getPlacesLived()
返回具有getValue()和getPrimary()方法的对象数组。列出它们:foreach($me->getPlacesLived()为$place)print\r($place->getValue())代码>
$me = $plus->people->get('me', array('fields' => 'id,gender,name,image,placesLived'));