Hun*_*ell 4 php youtube-api google-plus google-api-php-client
首先,我只想说出我需要从用户那里得到什么信息.
为了获得所有这些信息,我继续下载/安装了位于此处的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'));
Run Code Online (Sandbox Code Playgroud)
出于某种原因,当我运行这个:$myData = $GoogleData->userinfo->get('me');我得到这个:
Undefined property: Google_Service_Plus::$userinfo in path/test.php on line 61
不太确定我做错了什么,或者即使我应该使用Google Plus Api来获取这些信息.
我需要主帐户电子邮件(包含所有YouTube频道)和用户地址等.他们在Google帐户中注册了该电子邮件.我如何得到这些信息以及我在上面的例子中做错了什么?
我实际上创建了一个专门用于所有Google API的聊天室(转到这里)
这也让我想到了另一点.为什么文档过时而且没有得到解决.我看到的大多数例子都来自两年前,但我使用的是几个月前更新的内容.
使用新版本的PHP库(目前在GitHub上为 1.0.4-beta ):
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);
Run Code Online (Sandbox Code Playgroud)
范围应该是" https://www.googleapis.com/auth/plus.login "(我只测试了"个人资料"范围,因为我没有Google Plus个人资料).
要获取YouTube频道,您必须添加范围" https://www.googleapis.com/auth/youtube "并使用频道#list方法,并将'mine'参数设置为true.PHP lib中的类是'Google_Service_YouTube'.
我遇到了类似的问题并使用google-api-php-client的 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();
Run Code Online (Sandbox Code Playgroud)
假设返回有效的身份验证代码redirect_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;
Run Code Online (Sandbox Code Playgroud)
但是,不会返回位置.新的YouTube帐户没有YouTube特定的用户名
| 归档时间: |
|
| 查看次数: |
5219 次 |
| 最近记录: |