Google API: automatic calendar reading. How to enter?

I thought it would be a breeze. So I got stuck at the very beginning: - (

I created a Google calendar in which users can add events using login sharing. Then I need to write a PHP script on my server to read events over a specific period of time.

I thought the API key described here would be enough. But this is not so.

 curl https://www.googleapis.com/calendar/v3/users/me/calendarList?key=mykey

says Login required.

So, I read about OAuth2.0 and how I need to authenticate the user. The problem is that my script is not interactive (although hardcoding the login in the script is not a problem for me: the information is not critical to life). Therefore, I read about service accounts, but this is like non-user information.

Question: How can I encode my script to force username without involving a person?

Note: This SO question was indicative, but the answer is for API version 2.0, which seems deprecated.

+3
source share
2 answers

, , . . , PHP API Google, script ( ).

. . , Google API Access.

require_once '../../src/apiClient.php';
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));

$client = new apiClient();
// Visit https://code.google.com/apis/console to create your client id and cient secret
$client->setClientId('INSERT_CLIENT_ID');
$client->setClientSecret('INSERT_CLIENT_SECRET');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setScopes(array(
    'https://www.googleapis.com/auth/calendar',
    'https://www.googleapis.com/auth/calendar.readonly',
));

$authUrl = $client->createAuthUrl();

print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));

$_GET['code'] = $authCode;
$token = $client->authenticate();
var_dump($token);

json , accessToken refreshToken. accessToken 1 , . RefreshToken ( ) refreshToken. .

json- ( access_token , . $client->setAccessToken($token), $token location ( , $token - json- , access_token).

API- !

require_once '../../src/apiClient.php';
require_once '../../src/contrib/apiCalendarService.php';
session_start();

$client = new apiClient();
$client->setApplicationName("Google Calendar PHP Sample Application");
$cal = new apiCalendarService($client);

$client->setAccessToken($tokenFromSafePlace);

$calList = $cal->calendarList->listCalendarList();
print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";
+7

( v3 api?), :

require_once 'Google/Client.php';
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));

$client = new Google_Client();
// Visit https://console.developers.google.com/ to create your application and client id for a native app.
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setScopes(array(
    'https://www.googleapis.com/auth/calendar',
    'https://www.googleapis.com/auth/calendar.readonly',
));

$authUrl = $client->createAuthUrl();

print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));

$_GET['code'] = $authCode;
$token = $client->authenticate($authCode);
var_dump($token);

, :

require_once 'Google/Client.php';
require_once 'Google/Service/Calendar.php';

$client = new Google_Client();
$client->setApplicationName("My Calendar example");
// token you got from the previous code
$client->setAccessToken($token);

$calendarService = new Google_Service_Calendar($client);
$calendarList = $calendarService->calendarList;

$calList = $calendarList->listCalendarList();
print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";
+1

All Articles