{"id":282,"date":"2017-01-05T05:17:33","date_gmt":"2017-01-05T05:17:33","guid":{"rendered":"http:\/\/www.gmass.co\/blog\/?p=282"},"modified":"2019-10-17T14:29:01","modified_gmt":"2019-10-17T14:29:01","slug":"call-the-gmail-api-in-net-without-using-client-library","status":"publish","type":"post","link":"https:\/\/www.gmass.co\/blog\/call-the-gmail-api-in-net-without-using-client-library\/","title":{"rendered":"How to call the Gmail API in .NET without using the client library"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left;\">\n<p><i>This article is geared towards software developers interested in the Gmail API.<\/i><\/p>\n<p>GMass makes extensive use of two APIs &#8212; the <a href=\"https:\/\/www.inboxsdk.com\/\">Inbox SDK<\/a> API, made by Streak, and the <a href=\"https:\/\/developers.google.com\/gmail\/api\/\">Gmail API<\/a> provided by Google. The Gmail API manages all of the backend operations for GMass, including applying the &#8220;GMass Scheduled&#8221; Label to pending campaigns and actually sending a user&#8217;s mail merge campaign. GMass is built on the .NET platform and all backend code is written in C#. Google provides a <a href=\"https:\/\/developers.google.com\/api-client-library\/dotnet\/apis\/gmail\/v1\">.NET client library for the Gmail API<\/a>, but recently I discovered a bug in the client library that was a show-stopper, until I figured out how to call the Gmail API using raw HTTP calls.<\/p>\n<p>While the Gmail API asserts\u00a0that a Draft up to 35 MB in size can be created, I was getting an error from the .NET client library anytime I attempted to create a Draft greater than 1 MB in size. The error\u00a0was:<\/p>\n<pre><span style=\"color: #ff0000;\">Message[<span class=\"il\">Request<\/span>\u00a0<span class=\"m_-1251577692095186594gmail-il\"><span class=\"il\">payload<\/span><\/span>\u00a0size exceeds the limit: 1048576 bytes.]<\/span><\/pre>\n<p>I would only get this error when creating a large Draft with the .NET library. Testing the Users.drafts.create method manually using the <a href=\"https:\/\/developers.google.com\/gmail\/api\/v1\/reference\/users\/drafts\/create\">test form provided by Google<\/a> did allow me to create large\u00a0Drafts, which confirmed that the issue was with the .NET library. I therefore set out to create my own function to call the Gmail API to create a Draft and\u00a0bypass the functionality of the client library.<\/p>\n<p>Like many popular APIs, the Gmail API has an endpoint that you can make an HTTP POST to. In the case of the drafts.Create method, that endpoint is:<\/p>\n<pre>https:\/\/www.googleapis.com\/gmail\/v1\/users\/{EmailAddress}\/drafts<\/pre>\n<p>where <strong>{EmailAddress}<\/strong> is the Gmail account address for which you want to create the Draft.<\/p>\n<p>Here is the C# code I used to call <strong>service.Users.Drafts.Create<\/strong> manually:<\/p>\n<pre class=\"p1\"><span style=\"color: #0000ff;\"><span class=\"s1\">string<\/span> payload = <span class=\"s2\">\"{\\n\\\"message\\\": {\\n\"<\/span> + (!<span class=\"s1\">string<\/span>.IsNullOrEmpty(ThreadID) ? (<span class=\"s2\">\"\\\"threadId\\\":\\\"\"<\/span> + ThreadID + <span class=\"s2\">\"\\\",\\n\"<\/span>) : (<span class=\"s2\">\"\"<\/span>)) + <span class=\"s2\">\"\\\"raw\\\":\\n\\\"\"<\/span> + RawDraft + <span class=\"s2\">\"\\\"\\n}\\n}\"<\/span>;<\/span>\r\n<span style=\"color: #0000ff;\"><span class=\"s1\">using<\/span> (<span class=\"s1\">var<\/span> client = <span class=\"s1\">new<\/span> <span class=\"s3\">HttpClient<\/span>())<\/span>\r\n<span style=\"color: #0000ff;\">{<\/span>\r\n<span style=\"color: #0000ff;\"><span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0<\/span><span class=\"s1\">var<\/span> content = <span class=\"s1\">new<\/span> <span class=\"s3\">StringContent<\/span>(payload, <span class=\"s3\">Encoding<\/span>.Default, <span class=\"s2\">\"message\/rfc822\"<\/span>);<\/span>\r\n\r\n<span style=\"color: #0000ff;\"><span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0<\/span><span class=\"s1\">var<\/span> response = client.PostAsync(<span class=\"s2\">$\"https:\/\/www.googleapis.com\/gmail\/v1\/users\/<\/span>{EmailAddress}<span class=\"s2\">\/drafts?access_token=<\/span>{OAuthKey}<span class=\"s2\">\"<\/span>, content).Result;<\/span>\r\n<span style=\"color: #0000ff;\"><span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0<\/span><span class=\"s1\">string<\/span> resultContent = response.Content.ReadAsStringAsync().Result;<\/span>\r\n<span style=\"color: #0000ff;\"><span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0<\/span><span class=\"s1\">return<\/span> resultContent;<\/span>\r\n<span style=\"color: #0000ff;\">}\r\n<\/span><\/pre>\n<p>Using the HttpClient object will be familiar to most .NET developers, but the differentiating part of the call is that the <strong>mediaType<\/strong> has to be set to &#8220;message\/rfc822&#8221;, as that is what the Gmail API requires.<\/p>\n<p>Also note the payload is formatted according to how the payload is formatted on the <a href=\"https:\/\/developers.google.com\/gmail\/api\/v1\/reference\/users\/drafts\/create\">Google test form for this method<\/a>. In my code it&#8217;s assumed that:<\/p>\n<ul>\n<li><strong>ThreadID<\/strong> is the Gmail Thread ID if the created Draft is to be part of an existing thread, or non-existent\u00a0if not<\/li>\n<li><strong>RawDraft<\/strong> is a base 64 encoded string of the fully formatted email draft. If you want to see an example of a base 64 encoded Draft from your own Gmail account, use the <a href=\"https:\/\/developers.google.com\/gmail\/api\/v1\/reference\/users\/drafts\/get\">Users.drafts.get method test form<\/a> to plug in a Draft Id from your own account, set the Format to RAW, and retrieve the base 64 string.<\/li>\n<li><strong>EmailAddress<\/strong> is the Gmail account address for whom we&#8217;re creating the Draft.<\/li>\n<li><strong>OAuthKey<\/strong> is a string containing a valid unexpired OAuth 2.0 key to access the Gmail account of <strong>EmailAddress<\/strong>.<\/li>\n<\/ul>\n<p>After calling the Gmail API, <strong>resultContent<\/strong> will contain a string containing the created Draft ID and its Message ID, and these values can easily be extracted with this code:<\/p>\n<pre class=\"p1\"><span style=\"color: #0000ff;\"><span class=\"s1\">dynamic<\/span> GmailDraftResult = <span class=\"s2\">JsonConvert<\/span>.DeserializeObject(resultContent);<\/span><\/pre>\n<p class=\"p1\">You can then access the Gmail Draft ID with <strong>GmailDraftResult.id<\/strong> and the Gmail Message ID with <strong>GmailDraftResult.message.id<\/strong>.<\/p>\n<p class=\"p1\">The code above can be adapted to call any Gmail API method for which the .NET client library doesn&#8217;t work.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This article is geared towards software developers interested in the Gmail API. GMass makes extensive use of two APIs &#8212; the Inbox SDK API, made by Streak, and\u2026<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3462,5916],"tags":[],"class_list":["post-282","post","type-post","status-publish","format-standard","hentry","category-developers","category-gmail-api"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\r\n<title>How To Call the Gmail API in .NET W\/O The Client Library<\/title>\r\n<meta name=\"description\" content=\"Have you ever wondered how to call Gmail API without using the client library? Click here to learn more about API&#039;s and the backend operations for GMass!\" \/>\r\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/www.gmass.co\/blog\/call-the-gmail-api-in-net-without-using-client-library\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_US\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"How To Call the Gmail API in .NET W\/O The Client Library\" \/>\r\n<meta property=\"og:description\" content=\"Have you ever wondered how to call Gmail API without using the client library? Click here to learn more about API&#039;s and the backend operations for GMass!\" \/>\r\n<meta property=\"og:url\" content=\"https:\/\/www.gmass.co\/blog\/call-the-gmail-api-in-net-without-using-client-library\/\" \/>\r\n<meta property=\"og:site_name\" content=\"GMass Blog\" \/>\r\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GmailMailMerge\/\" \/>\r\n<meta property=\"article:published_time\" content=\"2017-01-05T05:17:33+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2019-10-17T14:29:01+00:00\" \/>\r\n<meta name=\"author\" content=\"Ajay Goel\" \/>\r\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\r\n<meta name=\"twitter:creator\" content=\"@PartTimeSnob\" \/>\r\n<meta name=\"twitter:site\" content=\"@GMassForGmail\" \/>\r\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ajay Goel\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/call-the-gmail-api-in-net-without-using-client-library\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/call-the-gmail-api-in-net-without-using-client-library\\\/\"},\"author\":{\"name\":\"Ajay Goel\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/#\\\/schema\\\/person\\\/b5fa74f8765b860701158fd77162fff8\"},\"headline\":\"How to call the Gmail API in .NET without using the client library\",\"datePublished\":\"2017-01-05T05:17:33+00:00\",\"dateModified\":\"2019-10-17T14:29:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/call-the-gmail-api-in-net-without-using-client-library\\\/\"},\"wordCount\":544,\"commentCount\":1,\"articleSection\":[\"Developers\",\"Gmail API\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.gmass.co\\\/blog\\\/call-the-gmail-api-in-net-without-using-client-library\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/call-the-gmail-api-in-net-without-using-client-library\\\/\",\"url\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/call-the-gmail-api-in-net-without-using-client-library\\\/\",\"name\":\"How To Call the Gmail API in .NET W\\\/O The Client Library\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/#website\"},\"datePublished\":\"2017-01-05T05:17:33+00:00\",\"dateModified\":\"2019-10-17T14:29:01+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/#\\\/schema\\\/person\\\/b5fa74f8765b860701158fd77162fff8\"},\"description\":\"Have you ever wondered how to call Gmail API without using the client library? Click here to learn more about API's and the backend operations for GMass!\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/call-the-gmail-api-in-net-without-using-client-library\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.gmass.co\\\/blog\\\/call-the-gmail-api-in-net-without-using-client-library\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/call-the-gmail-api-in-net-without-using-client-library\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to call the Gmail API in .NET without using the client library\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/\",\"name\":\"GMass Blog\",\"description\":\"Tips and tricks for sending mail merge and mass email campaigns directly from Gmail\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/#\\\/schema\\\/person\\\/b5fa74f8765b860701158fd77162fff8\",\"name\":\"Ajay Goel\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f3a70af05bbabe47925150d5a1320540e801b78055a74da20658fc97cd0706a2?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f3a70af05bbabe47925150d5a1320540e801b78055a74da20658fc97cd0706a2?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f3a70af05bbabe47925150d5a1320540e801b78055a74da20658fc97cd0706a2?s=96&d=mm&r=g\",\"caption\":\"Ajay Goel\"},\"description\":\"Ajay is the founder of GMass and has been developing email sending software for 20 years.\",\"sameAs\":[\"https:\\\/\\\/twitter.com\\\/PartTimeSnob\",\"https:\\\/\\\/x.com\\\/PartTimeSnob\"],\"url\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/author\\\/ajay-goel\\\/\"}]}<\/script>\r\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How To Call the Gmail API in .NET W\/O The Client Library","description":"Have you ever wondered how to call Gmail API without using the client library? Click here to learn more about API's and the backend operations for GMass!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.gmass.co\/blog\/call-the-gmail-api-in-net-without-using-client-library\/","og_locale":"en_US","og_type":"article","og_title":"How To Call the Gmail API in .NET W\/O The Client Library","og_description":"Have you ever wondered how to call Gmail API without using the client library? Click here to learn more about API's and the backend operations for GMass!","og_url":"https:\/\/www.gmass.co\/blog\/call-the-gmail-api-in-net-without-using-client-library\/","og_site_name":"GMass Blog","article_publisher":"https:\/\/www.facebook.com\/GmailMailMerge\/","article_published_time":"2017-01-05T05:17:33+00:00","article_modified_time":"2019-10-17T14:29:01+00:00","author":"Ajay Goel","twitter_card":"summary_large_image","twitter_creator":"@PartTimeSnob","twitter_site":"@GMassForGmail","twitter_misc":{"Written by":"Ajay Goel","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.gmass.co\/blog\/call-the-gmail-api-in-net-without-using-client-library\/#article","isPartOf":{"@id":"https:\/\/www.gmass.co\/blog\/call-the-gmail-api-in-net-without-using-client-library\/"},"author":{"name":"Ajay Goel","@id":"https:\/\/www.gmass.co\/blog\/#\/schema\/person\/b5fa74f8765b860701158fd77162fff8"},"headline":"How to call the Gmail API in .NET without using the client library","datePublished":"2017-01-05T05:17:33+00:00","dateModified":"2019-10-17T14:29:01+00:00","mainEntityOfPage":{"@id":"https:\/\/www.gmass.co\/blog\/call-the-gmail-api-in-net-without-using-client-library\/"},"wordCount":544,"commentCount":1,"articleSection":["Developers","Gmail API"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.gmass.co\/blog\/call-the-gmail-api-in-net-without-using-client-library\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.gmass.co\/blog\/call-the-gmail-api-in-net-without-using-client-library\/","url":"https:\/\/www.gmass.co\/blog\/call-the-gmail-api-in-net-without-using-client-library\/","name":"How To Call the Gmail API in .NET W\/O The Client Library","isPartOf":{"@id":"https:\/\/www.gmass.co\/blog\/#website"},"datePublished":"2017-01-05T05:17:33+00:00","dateModified":"2019-10-17T14:29:01+00:00","author":{"@id":"https:\/\/www.gmass.co\/blog\/#\/schema\/person\/b5fa74f8765b860701158fd77162fff8"},"description":"Have you ever wondered how to call Gmail API without using the client library? Click here to learn more about API's and the backend operations for GMass!","breadcrumb":{"@id":"https:\/\/www.gmass.co\/blog\/call-the-gmail-api-in-net-without-using-client-library\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.gmass.co\/blog\/call-the-gmail-api-in-net-without-using-client-library\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.gmass.co\/blog\/call-the-gmail-api-in-net-without-using-client-library\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.gmass.co\/blog\/"},{"@type":"ListItem","position":2,"name":"How to call the Gmail API in .NET without using the client library"}]},{"@type":"WebSite","@id":"https:\/\/www.gmass.co\/blog\/#website","url":"https:\/\/www.gmass.co\/blog\/","name":"GMass Blog","description":"Tips and tricks for sending mail merge and mass email campaigns directly from Gmail","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.gmass.co\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.gmass.co\/blog\/#\/schema\/person\/b5fa74f8765b860701158fd77162fff8","name":"Ajay Goel","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f3a70af05bbabe47925150d5a1320540e801b78055a74da20658fc97cd0706a2?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f3a70af05bbabe47925150d5a1320540e801b78055a74da20658fc97cd0706a2?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f3a70af05bbabe47925150d5a1320540e801b78055a74da20658fc97cd0706a2?s=96&d=mm&r=g","caption":"Ajay Goel"},"description":"Ajay is the founder of GMass and has been developing email sending software for 20 years.","sameAs":["https:\/\/twitter.com\/PartTimeSnob","https:\/\/x.com\/PartTimeSnob"],"url":"https:\/\/www.gmass.co\/blog\/author\/ajay-goel\/"}]}},"_links":{"self":[{"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/posts\/282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/comments?post=282"}],"version-history":[{"count":12,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/posts\/282\/revisions"}],"predecessor-version":[{"id":555,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/posts\/282\/revisions\/555"}],"wp:attachment":[{"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/media?parent=282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/categories?post=282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/tags?post=282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}