获取用户信息Google PHP客户端问题?

获取用户信息Google PHP客户端问题?,php,youtube-api,google-plus,google-api-php-client,Php,Youtube Api,Google Plus,Google Api Php Client,首先,我只想说我需要从用户那里得到什么信息 全名(名/姓) 电子邮件地址(主帐户,而不是@google-plus.com) 位置(国家、州、城市、地址) Youtube用户名 为了获得所有这些信息,我继续下载/安装了位于的PHP客户端库 由于这是我第一次使用API,我环顾四周,发现了以下范围: $client->setScopes(array('https://www.googleapis.com/auth/youtube.readonly', 'https://www.googlea

首先,我只想说我需要从用户那里得到什么信息

  • 全名(名/姓)
  • 电子邮件地址(主帐户,而不是@google-plus.com)
  • 位置(国家、州、城市、地址)
  • Youtube用户名
为了获得所有这些信息,我继续下载/安装了位于的PHP客户端库

由于这是我第一次使用API,我环顾四周,发现了以下范围:

$client->setScopes(array('https://www.googleapis.com/auth/youtube.readonly', 'https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.email'));
出于某种原因,当我运行此命令时:
$myData=$GoogleData->userinfo->get('me')我得到这个:

未定义的属性:Google\u Service\u Plus::$userinfo位于第61行的path/test.php中

不太确定我做错了什么,甚至不确定我是否应该使用GooglePlus Api来获取这些信息

我需要主帐户电子邮件(包含所有youtube频道)和用户地址等,他们在谷歌帐户中注册。我如何获得这些信息?在我的上述示例中,我做错了什么

实际上,我已经创建了一个聊天室,专门讨论Google API()的所有内容


这也使我想到另一点。为什么文档很早就过时了,而且没有得到重视。我看到的大多数示例都是两年前的,但我使用的是几个月前更新的内容。

使用新版本的PHP库(目前为1.0.4-beta):

范围应该是“”(我只测试了“profile”范围,因为我没有GooglePlus配置文件)


要获取YouTube频道,您必须添加作用域“”并使用方法,将“mine”参数设置为true。PHP库中的类是“Google_Service_YouTube”。

我遇到了一个类似的问题,并使用的1.1.4版解决了它

假设使用以下代码将用户重定向到Google身份验证页面:

 $client = new Google_Client();
 $client->setAuthConfigFile('/path/to/config/file/here');
 $client->setRedirectUri('https://redirect/url/here');
 $client->setAccessType('offline'); //optional
 $client->setScopes(['profile']); //or email
 $auth_url = $client->createAuthUrl();
 header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
 exit();
假设将有效的身份验证代码返回到
重定向\u url
,以下内容将从身份验证代码生成令牌,并提供基本配置文件信息:

 //assuming a successful authentication code is returned
 $authentication_code = 'code-returned-by-google';
 $client = new Google_Client();
 //.... configure $client object
 $client->authenticate($authentication_code);
 $token_data = $client->getAccessToken();

 //get user email address
 $google_oauth =new Google_Service_Oauth2($client);
 $google_account_email = $google_oauth->userinfo->get()->email;
 //$google_ouath->userinfo->get()->familyName;
 //$google_ouath->userinfo->get()->givenName;
 //$google_ouath->userinfo->get()->name;

但是,不会返回位置

尝试创建
Google\u Service\u OAuth2
服务而不是Plus,然后尝试通过
->get()
调用从
$Service->userinfo\u v2\u me
获取。@ircmaxell我需要添加什么范围谢谢。我会让你知道它是否有效。谢谢。请尝试查看源代码。这就是我所做的(我对API不熟悉):是的,它没有给我我想要的电子邮件。不是主帐户。
 //assuming a successful authentication code is returned
 $authentication_code = 'code-returned-by-google';
 $client = new Google_Client();
 //.... configure $client object
 $client->authenticate($authentication_code);
 $token_data = $client->getAccessToken();

 //get user email address
 $google_oauth =new Google_Service_Oauth2($client);
 $google_account_email = $google_oauth->userinfo->get()->email;
 //$google_ouath->userinfo->get()->familyName;
 //$google_ouath->userinfo->get()->givenName;
 //$google_ouath->userinfo->get()->name;