0

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!!

Nitesh
  • 2,286
  • 2
  • 43
  • 65

2 Answers2

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.

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
  • Thanks Mate!! I just figured, it returns a response as "Insert requests must contain an entry"..Any Idea? – Nitesh May 04 '11 at 18:31
  • That implies 1 of 2 things (that I can readily guess on) either 1. that's exactly what i said, and it's complaining about the lack of the CSRF tokens, or 2 you aren't using the correct parameters to send the information so the server thinks you haven't submitted data. Use a tool like `Fiddler` and do some HTTP tracing and look at the request and response messages back and forth from the server when you use the web page as a human. After getting the raw messages you should be able to see what you need to emulate. – Chris Marisic May 04 '11 at 18:34
  • Use Fiddler to replay requests and see if you can get them to succeed. – Chris Marisic May 04 '11 at 19:57
0

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;
        }
    }
Nitesh
  • 2,286
  • 2
  • 43
  • 65
  • I'm in your shoes in this very moment, but I'm not sure how should I build the request. If my project is foo, my username is bar and my password is foobar then what should be the URL? What class are you in, what is "this"? Thank you. – Lajos Arpad Nov 13 '12 at 18:18
  • Also, I have a question about this on stackoverflow.com: http://stackoverflow.com/questions/13365797/google-code-attached-issue – Lajos Arpad Nov 13 '12 at 18:19