我在使用php(zend框架)和Oauth更新用户封面图片时遇到了问题。
我在composer.json中添加了以下行:
"require" : {
"google/auth": "0.7",
"google/apiclient" : "^2.0.0@RC"
}
在那之后,我使用和oppp进行了composer安装+composer更新,我在我的供应商中获得了库。
我已经在谷歌开发控制台中配置了我的应用程序,遵循谷歌的官方教程: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,在bar地址中,我得到了一个新的变量"code"。
到目前为止,我希望一切都很好,进入最重要的部分,重定向URI页面的控制器,使用"代码"变量,我在尝试获取访问令牌之前已经讨论过它,但我失败了。
这是应该在谷歌加上设置新封面图片的方法:
$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();
但是我该怎么用呢?
我认为客户端库在检索信息时使用方法Google_Service_Plus_PersonCoverCoverPhoto()
和Google_Service_Plus_PersonCover()
来设置它。这并不意味着你可以在Google+上更新用户封面,如果这真的起作用,它只会更新类对象,这真的没有意义(IMO)。
var_dump($person->Cover);
如果你查看Plus.People文档,你会注意到没有更新或补丁方法。这是因为此时无法以编程方式更新用户信息。
回答:您无法更新用户封面图片与Oauth无关,而是API不允许这样做。不幸的是,看起来你做了很多工作都是徒劳的——这就是为什么在开始之前总是查阅文档是件好事——你会发现这是不可能的,并且可以避免给自己带来很多不必要的压力。