【发布时间】:2016-04-05 11:14:48
【问题描述】:
我在使用 php(zend 框架)和 Oauth 更新用户封面图片时遇到问题。
我在 composer.json 中添加了以下几行:
"require" : {
"google/auth": "0.7",
"google/apiclient" : "^2.0.0@RC"
}
之后,我使用和 oppp 制作了 composer-install + composer-update,我得到了我的供应商内部的库。
我已经按照谷歌官方教程在谷歌开发控制台中配置了我的应用程序:D
现在在我的控制器中,我可以使用这种方法轻松地请求谷歌网络服务:
public function googleplusAction()
{
Zend_Loader::loadFile("HttpPost.class.php");
$client_id = "id_here";
$client_secret = "secret_here";
$application_name = "application_name_here";
$redirect_uri = "redirection_uri_here";
$oauth2_server_url = 'https://accounts.google.com/o/oauth2/auth';
$query_params = array(
'response_type' => 'code',
// The app needs to use Google API in the background
'client_id' => $client_id,
'redirect_uri' => $redirect_uri,
'scope' => 'https://www.googleapis.com/auth/userinfo.profile'
);
$forward_url = $oauth2_server_url . '?' . http_build_query($query_params);
header('Location: ' . $forward_url);
}
之后,我被重定向到我的重定向 URI,并在栏地址中获得一个新变量“代码”。
直到现在,我希望一切都好,来到最重要的部分,重定向 URI 页面的控制器,使用我在尝试获取访问令牌之前讨论过的“代码”变量,但我失败了。
这是应该在google plus上设置新封面图片的方法:
$client_id = "client-id";
$client_secret = "g+-secret";
$application_name = "my-app-name";
$redirect_uri = "my-uri-on-g+";
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$service = new Google_Service_Oauth2($client);
$client->addScope(Google_Service_Oauth2::USERINFO_PROFILE);
$client->authenticate($_GET['code']); // I have the right code, and I am being authenticated
$plus = new Google_Service_Plus($client);
$person = $plus->people->get('me');
var_dump($person);
$pic = $this->session->image['generatedAbs'];
$gimg = new Google_Service_Plus_PersonCover();
$source = new Google_Service_Plus_PersonCoverCoverPhoto();
$source ->setUrl("$photo-that-i-wanted-to-put-on-g+");
$gimg->setCoverPhoto($source);
$person->setCover($gimg);}
所以我的问题是:
- 如何将我的 google plus 封面图片更改为项目中已有的新 png 或 JPEG 图片?
在 G+ 库中我找到了这个方法:
Google_Service_Plus_PersonCoverCoverPhoto();
在一个名为
的类中Google_Service_Plus_PersonCover();
但是我该如何使用它呢?
【问题讨论】:
标签: php zend-framework google-api google-plus