开发者

Upload file to a Google Site from C# Code

Any idea of how to upload a file to Google site fr开发者_Go百科om 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!!


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.


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;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜