-1

I have tried all the possible ways and solutions for the above error in all sites. Help me in this. Below is the code i have used. I have taken care with the authentication part because it is working fine for listing of drafts. The string "iDraftString" is the rfc2822 formatted string which includes everything, that is both body and headers. The same Raw64 while i use in the gmail explorer, it is creating draft successfully. So i dont think there is a problem with formatting the strings.

CreateDraft(string iDraftString)
{   
    string userId = "me@gmail.com";
    string uriString = "https://www.googleapis.com/gmail/v1/users/" + userId + "/drafts";
    Uri oLabelUri = new Uri(uriString);
    HttpWebRequest oSendReq = HttpWebRequest.CreateHttp(oLabelUri);
    oSendReq.Method = "POST";
    oSendReq.Headers["Authorization"] = "Bearer " + accessToken;
    StreamWriter oReqStream = new StreamWriter(oSendReq.GetRequestStream());
    string Raw64 = Base64Encode(iDraftString);
    string json = new JavaScriptSerializer().Serialize( new
    {
        message = new
        {
            raw = Raw64,
            sizeEstimate = Raw64.Length
        }
    });
        oReqStream.Write(json);
        oReqStream.Close();
        var oLabelResponse = (HttpWebResponse)oSendReq.GetResponse();
}

Error response

{  
   "error":{  
      "errors":[  
         {  
            "domain":"global",
            "reason":"invalid Argument",
            "message":"Missing draft message"
         }
      ],
      "code":400,
      "message":"Missing draft message"
   }
}
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Navin
  • 1
  • 1

1 Answers1

0

Finally i got the solution. The content-type of the Http request has to be set to "application/json"

oSendReq.ContentType = "application/json";

Navin
  • 1
  • 1