Laravel: Google contacts API gives empty results

情到浓时终转凉″ 提交于 2019-12-08 11:28:22

问题


I am trying to use Laravel 4.2 + OAuth2. I am using this package: ardarek-oauth-4-laravel for Laravel. Here are my related files.

Problem: Unable to retrieve Google contacts lists.

// Config - app/config/packages/artdarek/oauth-4-laravel/config.php - 'consumers'

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



// Controller

public function importGoogleContacts() {

    $code = Input::get('code'); // get data from input
    $googleService = OAuth::consumer('Google'); // get google service

    // if code is provided get user data and sign in
    if (!empty($code)) {
        Log::info('authorised');
        // This was a callback request from google, get the token
        $token = $googleService->requestAccessToken($code);

        // Send a request with it
        // $result = json_decode($googleService->request('https://www.googleapis.com/tasks/v1/lists/MDkyOTg5ODc5NDYw/tasks'), true);
        $result = json_decode($googleService->request('https://www.google.com/m8/feeds/contacts/default/full'), true);
        return Response::json($result);
    }
    // if not ask for permission first
    else {
        Log::info('not authorised');
        $url = $googleService->getAuthorizationUri(); // get googleService authorization
        Log::info('URL: ' . $url);
        return Redirect::to((string)$url); // return to google login url
    }
}

I tried userprofile and tasks APIs. Both are working fine - I am able to retrieve userProfile information and Tasks List. But, when I try for contacts, it echos an empty result. Any idea how to solve this problem? Is there any other package available for Laravel?

Thanks in advance - Anji


回答1:


The Google Contacts API returns XML by default, not JSON like many other Google APIs. To have it return JSON, append ?alt=json to the URL.



来源:https://stackoverflow.com/questions/26471562/laravel-google-contacts-api-gives-empty-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!