获取用户信息Google-PHP-Client问题? [英] Getting user info Google-PHP-Client issue?
问题描述
首先,我只想说说我需要从用户那里获得什么信息.
First, i just want to say what information i am needing to get from the user.
- 全名(姓/名)
- 电子邮件地址(主帐户,不是@ google-plus.com)
- 位置(国家,州,城市,地址)
- Youtube用户名
To get all of this information, i went ahead and download/installed the PHP-Client Library located here.
因为这是我第一次使用API,所以我环顾四周,发现了以下范围:
Since this is my first time using the API, i looked around and found the following scopes:
$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');
我得到了:
For some reason, when i run this: $myData = $GoogleData->userinfo->get('me');
i get this:
Undefined property: Google_Service_Plus::$userinfo in path/test.php on line 61
不太确定我在做什么错,或者即使我应该使用Google Plus Api来获取此信息.
Not too sure what i am doing wrong, or even if i should be using the Google Plus Api to get this information.
我需要主帐户电子邮件(包含所有youtube频道)和用户地址等.他们已在自己的Google帐户中进行了注册.我如何获得这些信息,在上面的示例中我在做什么错了?
I need the Main Account email (Contains all youtube channels) and the users address, etc. That they signed up in their Google Account. How do i get this information and what am i doing wrong in my above example?
我实际上已经创建了一个专门用于Google API的聊天室(转到此处)
I have actually created a chat room dedicated to all things Google API (Go here)
这也使我想到了另一点.为什么文档过时而又没有妥善处理.我看到的大多数示例都是两年前的,但是我使用的是几个月前更新的内容.
This also brings me to another point. Why is the documentation sooo outdated and not taken care of. Most of the examples that i see are from two years ago, but i am using something that has been updated a couple months ago.
推荐答案
Using the new version of the PHP library (currently 1.0.4-beta on GitHub):
require_once 'lib/Google/Client.php';
require_once 'lib/Google/Service/Plus.php';
$google_client = new \Google_Client;
$google_client->setClientId(GOOGLE_CLIENT_ID);
$google_client->setClientSecret(GOOGLE_CLIENT_SECRET);
$google_client->setRedirectUri(GOOGLE_REDIRECT_URI);
$google_client->setDeveloperKey(GOOGLE_DEVELOPER_KEY);
$google_client->setAccessType = 'offline';
// Either call:
// $google_client->authenticate($auth_code);
// with the $auth_code returned by the auth page or
// $google_client->setAccessToken($existing_token);
// with a previously generated access token.
$plus = new \Google_Service_Plus($google_client);
$person = $plus->people->get('me');
print_r($person);
范围应为" https://www.googleapis.com/auth/plus.login "(我只在个人资料"范围内进行过测试,因为我没有Google Plus个人资料).
The scope should be "https://www.googleapis.com/auth/plus.login" (I only tested with the "profile" scope, because I don't have a Google Plus profile).
要获取YouTube频道,您必须添加范围" https://www.googleapis .com/auth/youtube ",然后使用 channels#list 方法,并将"mine"参数设置为true. PHP库中的类为"Google_Service_YouTube".
To get the YouTube channels, you'd have to add the scope "https://www.googleapis.com/auth/youtube" and use the channels#list method, with the 'mine' parameter set to true. The class in the PHP lib is 'Google_Service_YouTube'.
这篇关于获取用户信息Google-PHP-Client问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!