I have the Google Client PHP library set up via Packagist and Composer.
I have successfully followed this getting started guide to verify that I can use the Google Client. I followed the example and was able to retrieve a list of books through the Books API, but that's not what I'm actually trying to do.
I am creating a website for a company, and they want to have a form on their website that will allow users to request a meeting at a certain time, and then this will be automatically added to said company's Google Calendar.
In the Google Calendar API, I see a lot of information about using OAuth to have a user sign in and grant access to THEIR calendar, which can then be used, however this is not what I want at all. The user shouldn't have to sign in; the server should gain access to the company's Calendar and then make requests to that SPECIFIC calendar.
I have made a Public Access API Key in the Google API Console and entered it in my code, i.e.
$client = new Google_Client();
$client->setApplicationName($applicationName);
$client->setDeveloperKey($developerKey);
$calendarService = new Google_CalendarService($client);
$calendars = $calendarService->calendars;
$results = $calendars->get($calendarId);
dd($results);
I learned of these methods by looking through the source. I have found that, at least on my Google Calendar, the $calendarId is simply my gmail address (found under Calendar Settings). However, this code returns an error:
Error calling GET
https://www.googleapis.com/calendar/v3/calendars/MY_EMAIL#gmail.comkey=MY_KEY_HERE: (401) Login Required
Of course, this is all wrong. I don't want the user to need to login. I just can't find any documentation on how to authenticate my app for this scenario. Every guide I come across assumes that the user will be able to sign in so I can access THEIR calendar. But I want the server to login and access a SPECIFIC calendar that belongs to the company.
Related
I am building a feature for a PHP Symfony app that will update calendar events in a Google Calendar upon changes in my database on my server. The calendar belongs to the google account info#my-domain.com which I administer (domain just illustrative).
I am following the instructions for the google-api-php-client in combination with the example for using this library with the calendar API.
Since this is not a multiuser scenario but pure server-to-server communication I opted for a service account on the Google Cloud Platform.
I created a project while logged in as info#my-domain.com and a service account. I downloaded the credentials.json for that service account. I enabled the Google Calendar API.
Now to the problem:
When I use this library this way:
$client = new \Google_Client();
$client->setAuthConfig('/config/google_api_keys.json');
$client->addScope(\Google_Service_Calendar::CALENDAR);
$cal_service = new \Google_Service_Calendar($client);
$results = $cal_service->calendarList->listCalendarList()->getItems();
dump($results); //basically a fancy symfony-version of print_r()
die();
the code throws no errors but the variable $results is empty even though it should contain 3 calendars.
Yet when I use the API Explorer while being logged in as info#my-domain.com the result of the same operation (GET https://www.googleapis.com/calendar/v3/users/me/calendarList) contains the 3 items I am looking for.
Just to check, I tried instead to use the PHP library to get the events for one of the 3 calendars using its id s2b[...]e81#group.calendar.google.com, but Google responded with 404-json containing "Not found".
One caveat with the API Explorer is that it only allows for login with OAuth 2.0 so seeing what might be wrong with a service account is not possible.
Does anyone see what I might be doing wrong? Or what I should try next? Or what concept I might have misunderstood when using the API?
possible cause for your issue one
Remember that listCalendarList is not automatically populated if you want a calendar to appear in listCalendarList then your going to have to insert it there. I suggest trying to do a listCalendarList.insert and trying to add the calendar id that you gave it access to.
If that returns an error then see possible cause for issue number to
possible cause for your issue two
I assume that you have set up domain wide deligation but you forgot to set which user you are delegating as you do that by setSubject
function initializeCalendar()
{
// Use the developers console and download your service account
// credentials in JSON format. Place them in this directory or
// change the key file location if necessary.
$KEY_FILE_LOCATION = __DIR__ . '/service-account-credentials.json';
$user_to_impersonate ="user#domain.com";
// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("Hello Calendar");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes(['https://www.googleapis.com/auth/calendar']);
$client->setSubject($user_to_impersonate);
$service = new Google_Service_Calendar($client);
return $service;
}
Without setting up delegation the service account does not have access to any calendars.
Okay, with the help of #daimto I could come to a very simple solution:
I had to actually share the calendar owned by info#my-domain.com with the service account by adding its email address abc-123#my-project-name.iam.gserviceaccount.com in the calendar settings, just like I'd share this calendar with any other physical person like eric#gmail.com.
So my understanding of what a service account actually is was seriously flawed. I thought that it would be the co-owner of any resource (docs, spreadsheets, calendars, etc) that project's owner owned.
WRONG: info#my-domain.com owns the project my-project-name which "owns" the service account abc-123#my-project-name.iam.gserviceaccount.com and therefore the service account owns "everything".
(MORE) RIGHT: The service account acts rather like a virtual user that first has to get access to any resource that it's supposed to be working with. The access sharing can be done like you would add a friend to a resource or via an API.
right now i'm using google-calendar-api with Laravel, i want to create a reminder, and let the user decides if he wants to shows it in his Google Calendar (based in his gmail account). Right now this code works fine:
I got an error when trying to add an attendee, Googling i found out that you need to "impersonate" the user you want to invite, so i tried the next approach:
And getting this error:
What do you guys think am i doing wrong?
In my Google Api Console i'm using a Service Account, i tried with several keys, i enabled Domain Wide Delegation:
Nothing works, as i said, i can create and list the events, but can't invite anyone.
Thanks in advance!
The error message
the client is not authorized to request a token using this method
Means that the client credetials that you created on Google developer console do not match the login method you are using
You apear to be using code designed for use with a service account
// Create and configure a new client object.
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope([YOUR SCOPES HERE]);
return $client;
To use this you need to create service account credentials on Google developer console. You have not.
I want to implement Google analytic functionality same as wordpress plugin provide https://wordpress.org/plugins/google-analytics-dashboard-for-wp/.
Is it possible to fetch google analytic data for any website by just using google analytics UA-11111-1 ID using core php without any authentication( like without passing client_id, client_secret etc..)?
To access data from the Google Analytics api we use the Core reporting api if you check the page you will notice it says.
All Requests to the Core Reporting API must be authorized, preferably
through OAuth 2.0.
Google Analytics Data is private data owned by the person is admin of the account. The person with admin access can give access to other people by adding there emails to the account.
In order to access Google Analytics data you need to be Authenticated. If you have access then you can use the API to access the data. You cant just take someone's analytics tracking code and grab data for there website. That would be like my being able to grab your Google drive documents or your private Google Photos.
Question: Is it possible Google analytic data for any website by just using Google analytics UA-11111-1 ID using core php without any authentication.
No. you must be authenticated.
But, If you have access to the analytics account in question you can use a service account which will not prompt for a user permission. I am really hoping this is what you meant to ask and not that you are trying to access random peoples Analytics data.
In the case of a wordpress plugin if you are releasing this for others then you should consider a service account. Your users will have to create there own service account in Google developer console, you cant release your client_id in a wordpress project. They can then add the service account to there Google Analytics account and will have access to there data on there wordpress site.
I would like to build an application that manages appointments in Google calendar on behalf of my users when they are not logged in.
The application would be a booking service, subscribing users (our service providers) would book out the times they are not available, and customers would see a free/busy representation of the service provider's google calendar
The application would need the ability to create, delete, and read appointments.
I have gone to the Google Developer's Console and have working code that pops up the Google Permissions screen, then redirects to a Url that successfully creates a new appointment.
I can access other calendar's but only if they are are logged into Google services at that time.
$client = new \Google_Client();
$client->addScope('https://www.googleapis.com/auth/calendar');
$service = new \Google_Service_Calendar($client);
$authUrl = $client->createAuthUrl();
This works okay if the user is managing their own Calendar. However, I want to manage appointments on their behalf. This would include others parties entering appointments into the calendar - i.e. when the calendar is not logged in.
What I need is an enduring authority for application to gain enduring access to my user's calendar?
Does anyone know if this possible? Thoughts as to how I could approach the problem would be appreciated.
OAuth 2.0 service accounts are the proper method for accessing user accounts:
https://developers.google.com/accounts/docs/OAuth2ServiceAccount
You need to grant the service account client id access to the necessary Calendar API scopes:
https://developers.google.com/drive/web/delegation#delegate_domain-wide_authority_to_your_service_account
Then your service account can impersonate your users and perform calendar operations on their behalf:
https://developers.google.com/drive/web/delegation#instantiate_a_drive_service_object
These docs describe Google Drive but the process for Calendar is identical.
I've downloaded the PHP client library for Google Adwords API. I need to insert my login details in /src/Google/Api/Ads/AdWords/auth.ini. One of the variable is developerToken.
How can I get it?
Directly from the API...
Your assigned Developer Token will be activated once your application
for API access is approved. Your token will be available through your
AdWords API Center—accessible through the My Account menu for the MCC
account you applied with. You'll be able to access the API by
including it in your request headers when interacting with our system.
It is very important that you keep your Contact Email up to date—we
may send you important information regarding disruptions to service
and urgent changes via this channel.
https://developers.google.com/adwords/api/docs/signingup
I understand this might be old, but since Google updated their API, let me share my experience.
You need to create an MCC account (My Client Center), basically if you already have a Google Adwords account you cannot use it to generate developerToken however, you still can you use same Gmail account to create a new MCC account.
Follow below:
https://adwords.google.com/home/tools/manager-accounts/
Click on Start Now
Probably you are already logged in using your Gmail account, in all cases use your Gmail account in the email address field.
Choose name for your account let's say main-account
Do not click save and continue
Check below picture, you need to click, click here in the message below.
You will then redirect to the main dashboard, where you can click on Tools and then under SETUP tab you will find Adwords API Centre where you will be able to generate developerToken
you have to use the developer token from your live account. The token will work fine even if its status is pending