Hi all,
I’m trying to get the the google calendar event list from photon. Google API describes this procedure here:
https://developers.google.com/identity/protocols/OAuth2ForDevices
basically you need to perform the following requests:
1.Request Code: this is a POST to https://accounts.google.com/o/oauth2/device/code with a form body including a “client_id” and “scope” for this request. Those dates are provided by your google developer console account.
The response to this request provide us with a “device_code” an “user_code”. The user code must be showed to the user to provide us with a calendar access permission under https://www.google.com/device
2.Check for approval: this is a POST to https://www.googleapis.com/oauth2/v4/token with a form body with your credentials (client_id, client_secret and device_code getting in the previous request). If the user accepted previously the permissions requested then this post provide us with an “access_token” valid during 3600 sec. Also provide a “refresh_token” for access renovation.
3.Finally we can get access to calendar events by a GET request to https://www.googleapis.com/calendar/v3/calendars/calendarId/events as described here
I can do the whole process under Postman without problems and this let me check all keys and values in each request.
An easy way to port this procedure to the photon firmware is with the help of webhooks. I’ll try to describe below the process that I follow without success:
To cover the first POST request described above I create a .json file:
{
"eventName": "request",
"url": "https://accounts.google.com/o/oauth2/device/code",
"requestType": "POST",
"headers": {
"Content-Type": "application/x-www-form-urlencoded"
},
"form": {
"client_id" : "xxxxxxxr.apps.googleusercontent.com",
"scope" : "https://www.googleapis.com/auth/calendar"
},
"mydevices": true
}
and declare in the Cloud using the particle CLI command $ particle webhook create request.son
the photon firmware is:
void setup() {
Serial.begin(9600);
Spark.subscribe("hook-response/request", requestprocess, MY_DEVICES);
for(int i=0;i<10;i++) {
Serial.println("waiting " + String(10-i) + " seconds before we publish");
delay(1000);
}
}
void loop() {
// Let's request the calendar pass but no more than once every 60 seconds.
Serial.println("Requesting Calendar pass!");
// publish the event that will trigger our Webhook
Spark.publish("request");
// and wait at least 60 seconds before doing it again
delay(60000);
}
void requestprocess(const char *name, const char *data) {
String str = String(data);
Serial.println("The response:\r\n " + str);
}
I get the following response:
before we publish
waiting 9 seconds before we publish
waiting 8 seconds before we publish
waiting 7 seconds before we publish
waiting 6 seconds before we publish
waiting 5 seconds before we publish
waiting 4 seconds before we publish
waiting 3 seconds before we publish
waiting 2 seconds before we publish
waiting 1 seconds before we publish
Requesting Calendar pass!
Requesting Calendar pass!
The response:
{
"error" : "invalid_request",
"error_description" : "Missing required parameter: scope"
}
Requesting Calendar pass!
The response:
{
"error" : "invalid_request",
"error_description" : "Missing required parameter: client_id"
}
Requesting Calendar pass!
The response:
{
"error" : "invalid_request",
"error_description" : "Missing required parameter: client_id"
}
Requesting Calendar pass!
The response:
{
"error" : "invalid_request",
"error_description" : "Missing required parameter: client_id"
}
Requesting Calendar pass!
Requesting Calendar pass!
The response:
{
"error" : "invalid_request",
"error_description" : "Missing required parameter: client_id"
}
The dashboard:
So, sorry about this detailed introduction but the issue is that I can get server connection but for any reason the body form with “scope” and “client_id” fields are not recognized by the server.
Please, any suggestion about?