artdarek / oauth-4-laravel

OAuth Service Provider for Laravel 4
684 stars 217 forks source link

How to get google contact list using artdarek-oauth-4-laravel? #112

Open Varun-Varunesh opened 9 years ago

Varun-Varunesh commented 9 years ago

I am using artdarek-oauth-4-laravel for the Login to my website via Facebook, twitter and google.

Login part is working fine. But I want to get some more data from these api, like if user is registering through the google then I am looking for their general info as well as google contact list, or if the user is registering from Facebook then I am trying to get the /me and /friend-list etc.

Here, I am just taking the case of google.

I have set the config like this

      'Google' => array(
           'client_id'     => '***********************************',
            'client_secret' => '***********************************',
            'scope'         => array('userinfo_email', 'userinfo_profile', 'https://www.google.com/m8/feeds/'),
       ),

My Controller Function is this:-

 public function loginWithGoogle()
 {
     $code = Input::get( 'code' );

     $googleService = OAuth::consumer( 'Google' );
     if ( !empty( $code ) )
     {
       $token = $googleService->requestAccessToken( $code );
    //    $result = json_decode( $googleService->request( 'https://www.googleapis.com/oauth2/v1/userinfo' ), true );
         $result = json_decode( $googleService->request( 'https://www.google.com/m8/feeds/contacts/default/full' ), true );
      echo json_encode($result);

     }
     else {
        // get googleService authorization
        $url = $googleService->getAuthorizationUri();
        // return to google login url
        return Redirect::to( (string)$url );
       }
    }

This code leads me to the google api and asks for all the permission that I have set in the scope of the service. Here once I got the access token after exchanging the code parameter with the api, I am calling the url to return me the contact list but it fails. And I am getting this message from the laravel :-Failed to request resource.

If I call the commented $result request, it returns me the result.

So, I wanted to know how can we use this library for the data other than login and register. In case of retrieving facebook friendlist the same thing happens but the login works. (My Facebook App has the permission to get friendlist).

Any help is appreciated.

mydnic commented 9 years ago

I've written an article on how to do it with Laravel 5, but you can easily adapt the code (it's exactly the same) with L4 and this package. http://mydnic.be/post/how-to-get-all-gmail-contacts-with-laravel-5