无法上传:在c#中使用Google Apis进行可恢复上传

时间:2018-01-18 14:27:06

标签: c# https google-api

我可以使用Resumable upload Google Mail API发送邮件。 我不想发送邮件,我只想在特定文件夹(标签)中插入邮件。

我试过了: 我使用OAUTH2获得了邮件服务。 &安培;获取令牌以创建HTTPRequest。 首先我创建了POST请求

string postUrl = "https://www.googleapis.com/upload/gmail/v1/users/ab@edu.cloudcodes.com/messages/send?uploadType=resumable";

                    HttpWebRequest f_httpRequest = (HttpWebRequest)WebRequest.Create(postUrl);

                    f_httpRequest.Headers["X-Upload-Content-Type"] = "message/rfc822";
                    f_httpRequest.Headers["X-Upload-Content-Length"] = fs.Length.ToString();
                    f_httpRequest.Headers["Authorization"] = "Bearer " + f_token;
                    f_httpRequest.Method = "POST";
                    f_httpRequest.ContentType = "application/json; charset=UTF-8";
                    f_httpRequest.ContentLength = fs.Length;
                    f_httpRequest.Timeout = 6000000;
                    f_httpRequest.SendChunked = true;      

从此请求的响应中获取上传URL。

上传URI:

  

https://www.googleapis.com/upload/gmail/v1/users/ab@edu.cloudcodes.com/messages/send?uploadType=resumable&upload_id=AEnB2UqF_xTLdmjKhXh1hY3wnT66nCelbOXlpGTv0dZB3Cgoy0Pl8Ki4yCcPdESzwSELFdq0CULLDbHjbve8AlzweHa2RZTtqg

通过发送总EML长度& chunk by chunk byte array to以下方法上传:

 private void CallPUTReq(long p_lenth, byte[] Arrbyte)
    {
        try
        {
            String postUrl = UploadUrl; //"https://www.googleapis.com/upload/gmail/v1/users/ab@edu.cloudcodes.com/messages/send?uploadType=resumable&upload_id=AEnB2UqZNtZVwWulAOhAVoFp-pZ-vTMcIXOpt_0dH_6jJecpm2Y1MNOGkE6JoDb0kn9Dt4yuHHMZWR--dBncxWQkZctF9h6jiPSL5uJDKeYE9Ut1c7-fImc";
            int EndOffset = 0;
            HttpWebRequest httpRequest1 = (HttpWebRequest)WebRequest.Create(postUrl);
            httpRequest1.Method = "PUT";
            httpRequest1.ContentLength = Arrbyte.Length;
            if (rangeStartOffset == 0)
            {
                EndOffset = Arrbyte.Length - 1;
            }
            else
            {
                EndOffset = rangeStartOffset + Arrbyte.Length - 1;
                if (EndOffset > p_lenth)
                {

                    EndOffset = Convert.ToInt32(p_lenth);
                    httpRequest1.ContentLength = EndOffset - rangeStartOffset;
                }

            }//5120000;5242880
            httpRequest1.Headers["Content-Range"] = "bytes " + rangeStartOffset + "-" + EndOffset + "/" + p_lenth; //"bytes */" + p_lenth; //
            httpRequest1.ContentType = "message/rfc822";
            httpRequest1.Timeout = 6000000;

            UTF8Encoding encoding = new UTF8Encoding();
            Stream stream = httpRequest1.GetRequestStream();
            stream.Write(Arrbyte, 0, Arrbyte.Length);
            stream.Close();

            try
            {
                using (Stream f_ObjHttpStream = httpRequest1.GetRequestStream())
                {

                }
            }
            catch (Exception EX)
            {

            }
            WebResponse response1 = null;
            try
            {
                using (response1 = (HttpWebResponse)httpRequest1.GetResponse())
                {

                }
            }
            catch (Exception ex)
            {

               // UploadUrl = response1.Headers["Location"].ToString();
            }
            //4194303
            rangeStartOffset = EndOffset +1;
        }
        catch (Exception)
        {

        }
    }

使用此我可以发送邮件,但我如何才能将邮件插入特定文件夹? 请建议。 谢谢。

0 个答案:

没有答案