Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a Watch for my Calendar using Google Calendar API...However when i trying to create it is throwing an exception as "The remote server returned an error: (400) Bad Request."

What I have tried:

HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/calendar/v3/calendars/floristpanel@gmail.com/events/watch");
request1.Method = "POST";

request1.Headers.Add("Authorization", "Bearer " + accesstoken);
request1.ContentType = "application/json";

string postData1 = "type=web_hook&id=01234567-89ab-cdef-0123456789ab&address=https://www.hanafloralpos.com/notification.aspx";
byte[] bytes1 = Encoding.UTF8.GetBytes(postData1);
request1.ContentLength = bytes1.Length;

Stream requestStream1 = request1.GetRequestStream();
requestStream1.Write(bytes1, 0, bytes1.Length);

WebResponse response1 = request1.GetResponse();
Stream stream1 = response1.GetResponseStream();
StreamReader reader1 = new StreamReader(stream1);

var result1 = reader1.ReadToEnd();
stream1.Dispose();
reader1.Dispose();
Posted
Updated 6-Mar-17 19:06pm
v2

1 solution

I work with the Google Drive API but the principle is the same.

You need to not just look at the status code but also the response data returned. Here is an example of a Bad (Google Drive) Request:
POST https://www.googleapis.com/oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 65
Expect: 100-continue
Connection: Keep-Alive

client_id=&client_secret=&grant_type=refresh_token&refresh_token=
and google's response data
HTTP/1.1 400 Bad Request
Vary: X-Origin
Content-Type: application/json; charset=UTF-8
Date: Tue, 07 Mar 2017 06:02:05 GMT
Expires: Tue, 07 Mar 2017 06:02:05 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alt-Svc: quic=":443"; ma=2592000; v="36,35,34"
Accept-Ranges: none
Vary: Origin,Accept-Encoding
Transfer-Encoding: chunked

{
 "error": "invalid_request",
 "error_description": "Missing required parameter: refresh_token"
}
UPDATED

This is the API page for the endpoint that you are calling: Events: watch  |  Google Calendar API  |  Google Developers[^]

The document states that the request is a Json packet:
In the request body, supply data with the following structure:

{
  "id": string,
  "token": string,
  "type": string,
  "address": string,
  "params": {
    "ttl": string
  }
}
Your data is in the incorrect form. So you are correctly receiving the Parse Error response.

UPDATE 2 re: Authorization Error

You need to Authenticate first, to gain access to your private data, before you can post changes. Authentication is more than the code that you have provided for your problem. You need to complete the steps in the following link: [^]

Once you have resolved the Authorization Error, then you can try creating your webhook.
 
Share this answer
 
v3
Comments
vamsi2710 7-Mar-17 1:14am    
I am trying to create it programatically and i am not able to see the response results...Can you let me know where and how can i see my bad request details

Graeme_Grant 7-Mar-17 1:18am    
in the response body ... read the response like you would any data returned.
Graeme_Grant 7-Mar-17 1:19am    
if you want to "peek" at the data, then use a tool like: Fiddler free web debugging proxy[^]
vamsi2710 7-Mar-17 1:20am    
This is what i get in the Stack trace in the Response

at System.Net.HttpWebRequest.GetResponse()
at calendarquickstart.Program.Main(String[] args) in c:\users\haanapoc\documents\visual studio 2010\Projects\calendarquickstart\calendarquickstart\Program.cs:line 95
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
vamsi2710 7-Mar-17 1:32am    
HTTP/1.1 400 Bad Request
Vary: X-Origin
Content-Type: application/json; charset=UTF-8
Date: Tue, 07 Mar 2017 06:30:57 GMT
Expires: Tue, 07 Mar 2017 06:30:57 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alt-Svc: quic=":443"; ma=2592000; v="36,35,34"
Accept-Ranges: none
Vary: Origin,Accept-Encoding
Transfer-Encoding: chunked

ab
{
"error": {
"errors": [
{
"domain": "global",
"reason": "parseError",
"message": "Parse Error"
}
],
"code": 400,
"message": "Parse Error"
}
}

0

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900