google-api-php-client icon indicating copy to clipboard operation
google-api-php-client copied to clipboard

Post to Google Plus Wall(activities)

Open Chathula opened this issue 9 years ago • 14 comments

i tried everything, i searched a lot. but there is no good documentation about this. i tried using custom http request, i tried by using PlusDomains Service. but nothing work. can anyone give me a clear guide about this.

these are the ways i tried for now....

        $client = new Google_Client();
        // $client->useApplicationDefaultCredentials();
        $scopes = [Google_Service_Plus::PLUS_ME, Google_Service_PlusDomains::PLUS_STREAM_WRITE];

        $client->setAuthConfig(storage_path('app') . '/client_secrets.json');
        $client->setAccessType("offline");

        if($client->isAccessTokenExpired()) {
            $client->refreshToken($google->refresh_token);

            /*$authData = AuthData::find($google->id);
            $authData->access_token = $client->getAccessToken()['access_token'];
            $authData->access_secret = $client->getAccessToken()['id_token'];
            $authData->refresh_token = $client->getRefreshToken();
            $authData->save();*/ // this is something relevent to my laravel model works

            $client->setAccessToken($client->getAccessToken()['access_token']);
        }

        $client->setAccessToken($google->access_token);

        $http = $client->authorize();

        $client->setScopes($scopes);
        $plus = new Google_Service_Oauth2($client);
        $userId = $plus->userinfo->get()->id;

        $plusDomain = new Google_Service_PlusDomains($client);

        $activity = new Google_Service_PlusDomains_Activity();
        $obj = new Google_Service_PlusDomains_ActivityObject();
        $obj->setContent("Test text");
        $obj->setActor($userId);

        $activity->setObject($obj);
        $activity->setActor($userId);
        $activity->setTitle("title");


        return $plusDomain->activities->insert($userId, $activity);

        // $url = sprintf('https://www.googleapis.com/plusDomains/v1/people/%s/activities', $userId);
        //
        // $headers = ['content-type' => 'application/json'];
        //

        //
        // $httpRequest = new \GuzzleHttp\Psr7\Request('POST', $url, $headers, json_encode($body));
        //
        // // make the HTTP request
        // $response = $http->send($httpRequest);
        //
        // return $response;

Chathula avatar Apr 08 '17 08:04 Chathula

i tried again.. i can see the requests on Google API Console. but it shows client errros... this is what i get when i try to work...

Google_Service_Exception in REST.php line 118:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "forbidden",
"message": "Forbidden"
}
],
"code": 403,
"message": "Forbidden"
}
}

Chathula avatar Apr 08 '17 10:04 Chathula

Hello, I have the same problem as you. Also "Forbidden" status. Have you found any solution? Thank you in advance

ir3nicus avatar Apr 27 '17 09:04 ir3nicus

@ir3nicus, we can't post through the normal gmail account.we have to use a gsuite account to get full acess for plusDomain features. i think that we have to access google api through that gsuite account email.

Chathula avatar Apr 27 '17 14:04 Chathula

@ir3nicus It is most likely the userId you are using does not have access to the Plus Domains API.

bshaffer avatar May 01 '17 16:05 bshaffer

Hello, ok thank you for replay. When I access Plus Domain API.( I launched new app from Google Suite Account). Then every Google user can access it or only users that has Google Suite Account too?

2017-05-01 18:44 GMT+02:00 Brent Shaffer [email protected]:

Closed #1195 https://github.com/google/google-api-php-client/issues/1195 .

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/google/google-api-php-client/issues/1195#event-1064178593, or mute the thread https://github.com/notifications/unsubscribe-auth/ADtoqEc_De9pcGOiWvggZsEJpuxM9oGSks5r1gv1gaJpZM4M3oqM .

--

Pozdrawiam Marcin Mazanek

ir3nicus avatar May 01 '17 20:05 ir3nicus

It doesn't matter who creates the Google Console API app. What matters is that the user whose credentials are being used for the request is a paid G Suite user. Last I checked the posted content had to be private to that company as well.

abraham avatar May 07 '17 14:05 abraham

@abraham so then how buffer, hootsuite type websites post the content to the G+. i wanted to do the same thing...

Chathula avatar May 07 '17 15:05 Chathula

Google has a handful of partners that have elevated access.

abraham avatar May 07 '17 16:05 abraham

@abraham can i/our company get that too?

Chathula avatar May 07 '17 17:05 Chathula

As far as I know Google will reach out to any companies they are interested in adding to the program.

abraham avatar May 07 '17 17:05 abraham

Just spent a good amount of time reading the G+ docs (likely not well enough) and getting all the OAuth dance, API Console Project stuff, etc, out of the way...only to find that all attempts to use any access_token granted to my app/credentials would be responded to with this same 403 Forbidden:

{
    "error": {
        "errors": [
            {
                "domain": "global",
                "reason": "forbidden",
                "message": "Forbidden"
            }
        ],
        "code": 403,
        "message": "Forbidden"
    }
}

If I'm understanding correctly what @abraham and @bshaffer have been saying it's that this will never work unless:

  1. I have a G Suite domain set up for my domain (let's say it's example.com).
  2. I have connected my App and my domain example.com to one another.
  3. The User who's gone through the OAuth process and granted my app permissions must be an @example.com User. OR
  4. I am considered "special" by Google and elevated permissions are granted to my app allowing for free users with @gmail.com, etc, domains to be accessed by my app.

Is that correct, and is it not enough to do steps 1. and 2. only? If so, I'm afraid this would render all my work on this useless since we'd want to allow arbitrary Users to grant our app access to do things on their behalf.

newhouse avatar Aug 22 '17 00:08 newhouse

2 isn't needed. Assuming a G Suite organization is configured their users can us any app and post to their domain's Google+ via that app.

But basically yes. @gmail.com users can't generally post to Google+ through the API.

abraham avatar Aug 22 '17 02:08 abraham

Hi, any idea on how to contact google to become 'special' under point 4 of @newhouse post?

gasserol avatar Dec 01 '17 16:12 gasserol

I need an account in apps.google.com, soo i m going to buy to post in google plus. Which plan i need to buy.???

BilalBasha avatar Feb 09 '18 04:02 BilalBasha