Search code examples
phpzend-frameworkgoogle-apigoogle-plus

How to a users Google+ cover programticly


I had a problem with updating user cover pic using php(zend framework) and Oauth.

I have added to my composer.json the following lines:

"require" : {
"google/auth": "0.7",
"google/apiclient" : "^2.0.0@RC"
}

After that I made composer-install + composer-update using and oppp I get the library inside my vendor.

I have configured my application inside google developing console, following the official tutorial by google :D

Now inside my controller I could easily request google web service using this method :

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);
}

After that I get redirected to my redirection URI , and in the bar address I get a new variable 'code'.

Until now, I hope everything is fine , coming to the most important part , the controller of the redirection URI page , using the 'code' variable that I have talked about it before I tried to get an access token, but I was failed.

This is the method that should set a new cover picture on 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);}

So my questions are :

  1. How can I change my google plus cover picture to a new png or JPEG picture that I have already in my project ?

inside the G+ library I found this method :

Google_Service_Plus_PersonCoverCoverPhoto();

inside a class called

Google_Service_Plus_PersonCover(); 

But how can I use it ?


Solution

  • I think that methods Google_Service_Plus_PersonCoverCoverPhoto() and Google_Service_Plus_PersonCover() are used by the client library to set it when the information is retrieved. It is not meant for you to be able to update the users cover on Google+, if that does work it will only update the class object which really there is no point in doing (IMO).

    var_dump($person->Cover);
    

    If you check the Plus.People documentation you will notice there are no update or patch methods. This is because its not possible to update a users information programmatically at this time.

    Answer: Your inability to update the users cover picture has nothing to do with Oauth it has to do with the fact that this is not allowed by the API. Unfortunately it looks like you have done a lot of work for nothing this is why it is good to always consult the documentation before you begin you would have seen that it was not possible, and could have avoided a lot of unnecessary stress on yourself.