如何以编程方式覆盖用户 Google+
How to a users Google+ cover programticly
我在使用 php(zend framework) 和 Oauth 更新用户封面图片时遇到问题。
我在 composer.json 中添加了以下行:
"require" : {
"google/auth": "0.7",
"google/apiclient" : "^2.0.0@RC"
}
之后,我使用和 oppp 安装了 composer-install + composer-update,我在我的供应商中获得了库。
我已经在 google 开发控制台中配置了我的应用程序,遵循 google 的官方教程 :D
现在在我的控制器中,我可以使用此方法轻松请求 google 网络服务:
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,并且在栏地址中我得到一个新变量 'code'。
到现在为止,我希望一切都好,来到最重要的部分,重定向 URI 页面的控制器,使用我在尝试获取访问权限之前讨论过的 'code' 变量令牌,但我失败了。
这是应该在 google 上设置新封面图片的方法加上 :
$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();
在一个名为
的 class 中
Google_Service_Plus_PersonCover();
但是我该如何使用呢?
我认为客户端库使用方法Google_Service_Plus_PersonCoverCoverPhoto()
和Google_Service_Plus_PersonCover()
在检索信息时设置它。这并不意味着您能够更新 Google+ 上的用户封面,如果确实有效,它只会更新 class 对象,这实际上没有任何意义(IMO)。
var_dump($person->Cover);
如果您查看 Plus.People 文档,您会发现没有更新或补丁方法。这是因为此时无法以编程方式更新用户信息。
回答:您无法更新用户封面图片与 Oauth 无关,它与 API 不允许的事实有关.不幸的是,看起来你做了很多工作却一无所获,这就是为什么在开始之前总是查阅文档是很好的原因,你会发现这是不可能的,并且可以避免给自己带来很多不必要的压力。
我在使用 php(zend framework) 和 Oauth 更新用户封面图片时遇到问题。
我在 composer.json 中添加了以下行:
"require" : {
"google/auth": "0.7",
"google/apiclient" : "^2.0.0@RC"
}
之后,我使用和 oppp 安装了 composer-install + composer-update,我在我的供应商中获得了库。
我已经在 google 开发控制台中配置了我的应用程序,遵循 google 的官方教程 :D
现在在我的控制器中,我可以使用此方法轻松请求 google 网络服务:
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,并且在栏地址中我得到一个新变量 'code'。
到现在为止,我希望一切都好,来到最重要的部分,重定向 URI 页面的控制器,使用我在尝试获取访问权限之前讨论过的 'code' 变量令牌,但我失败了。
这是应该在 google 上设置新封面图片的方法加上 :
$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();
在一个名为
的 class 中Google_Service_Plus_PersonCover();
但是我该如何使用呢?
我认为客户端库使用方法Google_Service_Plus_PersonCoverCoverPhoto()
和Google_Service_Plus_PersonCover()
在检索信息时设置它。这并不意味着您能够更新 Google+ 上的用户封面,如果确实有效,它只会更新 class 对象,这实际上没有任何意义(IMO)。
var_dump($person->Cover);
如果您查看 Plus.People 文档,您会发现没有更新或补丁方法。这是因为此时无法以编程方式更新用户信息。
回答:您无法更新用户封面图片与 Oauth 无关,它与 API 不允许的事实有关.不幸的是,看起来你做了很多工作却一无所获,这就是为什么在开始之前总是查阅文档是很好的原因,你会发现这是不可能的,并且可以避免给自己带来很多不必要的压力。