Uwierzytelnianie w google: OAuth2 powraca „invalid_grant”
Zacząłem konfigurować kalendarz google na mojej nowej aplikacji. Prawie wykonałem dokładną kopię kodu uwierzytelniającego wyświetlanego u deweloperów google (https://developers.google.com/google-apps/calendar/instantiate ), ale otrzymuję następujący błąd:
Błąd pobierania tokenu dostępu OAuth2, komunikat: „invalid_grant”
Obecnie używam Fork-CMS (http://www.fork-cms.com ), młody lekki CMS. Prawidłowo skonfigurowałem plik config.php klienta google-api-php. (identyfikator klienta, tajny klient, przekierowanie-uri, klucz programistyczny, ...), a uri przekierowania jest poprawnie ustawiony na konsoli Google api. Mój kod wygląda następująco:
<code><?php /** * This is a widget with a calendar implementation. * * @package frontend * @subpackage events * * @author Michiel Vlaminck <[email protected]> */ class FrontendEventsWidgetCalendar extends FrontendBaseWidget { private $events = array(); private $authUrl = array(); /** * Execute the extra * * @return void */ public function execute() { // call parent parent::execute(); // load template $this->loadTemplate(); // get data $this->getData(); // parse $this->parse(); } /** * Get the data from Google Calendar * This method is only executed if the template isn't cached * * @return void */ private function getData() { require_once PATH_LIBRARY . '/external/google-api-php-client/src/apiClient.php'; require_once PATH_LIBRARY . '/external/google-api-php-client/src/contrib/apiCalendarService.php'; $client = new apiClient(); $service = new apiCalendarService($client); if (isset($_SESSION['oauth_access_token'])) { $client->setAccessToken($_SESSION['oauth_access_token']); } else { $token = $client->authenticate(); $_SESSION['oauth_access_token'] = $token; } if ($client->getAccessToken()) { $calId = FrontendEventsModel::getCalendarId((int) $this->data['id']); $calId = $calId[0]['calendar_id']; $events = $service->events->listEvents($calId); $this->events = $events['items']; $_SESSION['oauth_access_token'] = $client->getAccessToken(); } else { $this->authUrl = $client->createAuthUrl(); } } /** * Parse * * @return void */ private function parse() { $this->tpl->assign('events', $this->events); $this->tpl->assign('authUrl', $this->authUrl); } } ?> </code>
Kiedy po raz pierwszy otwieram tę stronę widżetu, otrzymuję polecenie do Google, aby uwierzytelnić aplikację. Kiedy się zgadzam, zostaje przekierowany do mojej aplikacji i to jest punkt, w którym otrzymuję:
<code>apiAuthException » Main Message Error fetching OAuth2 access token, message: 'invalid_grant' File C:\wamp\www\Officevibes\library/external\google-api-php-client\src\auth\apiOAuth2.php Line 105 Date Thu, 05 Apr 2012 08:34:47 +0000 URL http://localhost/calendar?code=4/YPUpFklKvhEeTcMm4moRth3x49oe Referring URL (Unknown) Request Method GET User-agent Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19 </code>