Upload file to a Google Site from C# Code

一世执手 提交于 2019-12-25 15:18:04

问题


Any idea of how to upload a file to Google site from c#?

I am trying to upload but getting a 403 error. However, I am using the same credentials to connect to the site and get the list of attachments and pages present on the site.

Any help would be greatly appreciated!!


回答1:


They most likely have an anti-CSRF scheme that stores temporal identifiers in the page and/or cookies, this is specifically to hinder bots. You are most likely submitting a request without the proper CSRF tokens and get rejected. I would recommend analyzing how they handle CSRF, after this point it will most likely boil down to making a WebRequest to the page and so you can get any cookies they get back, along with having the form so you can scrape out any hidden fields that are relevant. Then move those over to your post request that you're attempting to the send the file to.




回答2:


I figured out the problem and resolved it. Below is the complete function:

public bool UploadAttachment()
    {
        try
        {
            //AsyncSendData data = new AsyncSendData();

            string parentUrl = Cabinets["Cabinet1"].ToString();
            string parentID = parentUrl.Split('/')[7];

            AtomEntry entry = new AtomEntry();
            entry.Title.Text = "abc.jpg";

            AtomCategory cat = new AtomCategory();
            cat.Term = ATTACHMENT_TERM;
            cat.Label = "attachment";
            cat.Scheme = KIND_SCHEME;
            entry.Categories.Add(cat);

            AtomLink link = new AtomLink();
            link.Rel = PARENT_REL;
            link.HRef = parentUrl;
            entry.Links.Add(link);

            AtomContent content = new AtomContent();
            FileInfo info = new FileInfo("C:\\Bluehills.txt");
            FileStream stream = info.Open(FileMode.Open,FileAccess.ReadWrite,FileShare.ReadWrite);

            this.setUserCredentials(userName, password);
            Uri postUri = new Uri(makeFeedUri("content"));

            entry.Source = new AtomSource();
            //this.EntrySend(postUri, entry, GDataRequestType.Insert);
            // Send the request and receive the response:
            AtomEntry insertedEntry = this.Insert(postUri, stream, (string)DocumentTypes["TXT"], "bluehills");

            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }


来源:https://stackoverflow.com/questions/5888095/upload-file-to-a-google-site-from-c-sharp-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!