0

UploadAttachment + OAuth

Hi,
I'm trying to upload a simple text file using OAuth and UploadAttachment method, but I keep getting a 500 response.
I have found a single discussion regarding this issue here and followed the example without success.

Here's my C# code:

CODE
string url = @"http://mail.yahooapis.com/ya/upload";

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
OAuthUtils utils = new OAuthUtils();

AuthorizeHeader hdr = utils.GetUserInfoAuthorizationHeader(url, @"yahooapis.com", OAuthConsumerKey, OAuthConsumerSecret, OAuthAccessToken, OAuthTokenSecret, SignatureMethod.HMACSHA1, @"POST");
byte[] filedata = File.ReadAllBytes(path);
string base64filecontent = Convert.ToBase64String(filedata);

req.Headers.Add(@"Authorization", hdr.ToString());
req.Method = @"POST";

string boundary = @"---------------abcdefghij";
req.ContentType = string.Format("multipart/form-data;boundary={0}", boundary);

StringBuilder content = new System.Text.StringBuilder();
content.AppendLine(boundary);
content.AppendLine("Content-Disposition: form-data; name=\"_charset_\"");
content.AppendLine();
content.AppendLine(@"utf-8");
content.AppendLine(boundary);
content.AppendLine(string.Format("Content-Disposition:form-data; name=\"uploadfile\"; filename=\"{0}\"", path));
content.AppendLine(@"Content-Type: text/plain");
content.AppendLine();

content.AppendLine(base64filecontent);

content.AppendLine(boundary+@"--");

byte[] postdata = System.Text.Encoding.UTF8.GetBytes(content.ToString());
req.ContentLength = postdata.Length;

Stream s = req.GetRequestStream();
s.Write(postdata, 0, postdata.Length);

s.Flush();
s.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();


My authorization header seems to be fine (using same header for other methods without problems).
Any suggestions would be appreciated :-)
MK

by
6 Replies
  • Hi,

    Can you please paste the dump of the request /response? Thanks

    --R

    QUOTE (Robbie Cohen @ Jan 4 2011, 10:26 PM) <{POST_SNAPBACK}>
    Hi,
    I'm trying to upload a simple text file using OAuth and UploadAttachment method, but I keep getting a 500 response.
    I have found a single discussion regarding this issue here and followed the example without success.

    Here's my C# code:

    CODE
    string url = @"http://mail.yahooapis.com/ya/upload";

    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
    OAuthUtils utils = new OAuthUtils();

    AuthorizeHeader hdr = utils.GetUserInfoAuthorizationHeader(url, @"yahooapis.com", OAuthConsumerKey, OAuthConsumerSecret, OAuthAccessToken, OAuthTokenSecret, SignatureMethod.HMACSHA1, @"POST");
    byte[] filedata = File.ReadAllBytes(path);
    string base64filecontent = Convert.ToBase64String(filedata);

    req.Headers.Add(@"Authorization", hdr.ToString());
    req.Method = @"POST";

    string boundary = @"---------------abcdefghij";
    req.ContentType = string.Format("multipart/form-data;boundary={0}", boundary);

    StringBuilder content = new System.Text.StringBuilder();
    content.AppendLine(boundary);
    content.AppendLine("Content-Disposition: form-data; name=\"_charset_\"");
    content.AppendLine();
    content.AppendLine(@"utf-8");
    content.AppendLine(boundary);
    content.AppendLine(string.Format("Content-Disposition:form-data; name=\"uploadfile\"; filename=\"{0}\"", path));
    content.AppendLine(@"Content-Type: text/plain");
    content.AppendLine();

    content.AppendLine(base64filecontent);

    content.AppendLine(boundary+@"--");

    byte[] postdata = System.Text.Encoding.UTF8.GetBytes(content.ToString());
    req.ContentLength = postdata.Length;

    Stream s = req.GetRequestStream();
    s.Write(postdata, 0, postdata.Length);

    s.Flush();
    s.Close();
    HttpWebResponse res = (HttpWebResponse)req.GetResponse();


    My authorization header seems to be fine (using same header for other methods without problems).
    Any suggestions would be appreciated :-)
    MK
    0
    • Aug 28, 2012
    Hello guys, could any one of you help me with how i download attachment.
    please can you post a complete http request with url, query strings and authorization header....
    if c# code is exists will be very good.


    please help ASAP
    0
    • Sep 2, 2012
    Hello,,,, anyone did work on UploadAttachment with succeed ???
    0
  • Can you please explain the problem you are facing?
    0
    • Sep 5, 2012
    First of all, Thank you for reply...the scenario is: after struggling with download attachment endpoit, i got it work. :))), now i am working on upload attachment endpoint.
    what i have done is:
    1. i generate a web request object for this url: http://mail.yahooapis.com/ya/upload?uploadfile=" + file.FileName + "&output=json; with authorization header, same as download attachment web request.
    2. i convert the file content to array of bytes, and i save the file in my project local path.
    3. set the method to POST.
    4. and set the Content-Type in the header to : multipart/form-data.
    5. convert to the array of bytes to base64.
    6. write the content to a request stream
    ..and the error is FileUploadSizeError.
    i dont know why...

    i have tried many senarios,
    --sending full path of file in the questy string fileupload
    --sending with request stream and without writing into a stream.

    please help.

    this is my code: 

    /***Upload Attachment**/
     string dt = DateTime.Now.ToString("ddMMyyyyHHmmss");
                        file.SaveAs(Server.MapPath("/Uploaded/yahoo/"+dt+"-"+file.FileName));
                        
                        MemoryStream ms = new MemoryStream();
                        file.InputStream.CopyTo(ms);
                        byte[] datacontent = ms.ToArray();
                        ms.Dispose();
                        string base64filecontent = Convert.ToBase64String(datacontent);
                        HttpWebResponse response;
                        Stream stream;
                        Uri uri = new Uri("http://mail.yahooapis.com/ya/upload?uploadfile=" + file.FileName + "&output=json");
                        string oauth_token = yhModel.OAuth_AccessToken;
                        string oauth_secret = yhModel.OAuth_TokenSecret;

                        HttpWebRequest webrequest = yhModel.GetWebRequest(oauth_token, oauth_secret, uri, "POST");
                        webrequest.Method = "POST";
                        webrequest.ContentType = "multipart/form-data";
                        StringBuilder content = new System.Text.StringBuilder();
                        content.AppendLine(base64filecontent);
                        byte[] postdata = System.Text.Encoding.UTF8.GetBytes(content.ToString());
                        webrequest.ContentLength = postdata.Length;
                        Stream s = webrequest.GetRequestStream();
                        s.Write(postdata, 0, postdata.Length);
                        s.Flush();
                        s.Close();
                        HttpWebResponse res = (HttpWebResponse)webrequest.GetResponse();

    /**BuildWebRequest****/

     DataTable dtResult = MyIBoxModel.GetAPP_AccountCredentials(3);
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
                oAuthBase oauth = new oAuthBase();
                string nonce = oauth.GenerateNonce();
                string timestamp = oauth.GenerateTimeStamp();
                string consumerKey = dtResult.Rows[0]["APP_KEY"].ToString();
                string consumerSecret = dtResult.Rows[0]["APP_SECRET"].ToString();
                string p = "", h = "";
                string signature = oauth.GenerateSignature(uri, consumerKey, consumerSecret, oauth_token, oauth_secret, strMethod, timestamp, nonce,out p,out h);
                webRequest.Headers.Add("Authorization", "OAuth oauth_nonce=\"" + nonce + "\"" +
                    ", oauth_timestamp=\""+timestamp+"\""+
                    ", oauth_version=\"1.0"+"\""+
                    ", oauth_signature_method=\"HMAC-SHA1"+"\""+
                    ", oauth_consumer_key=\""+consumerKey+"\""+
                    ", oauth_token=\""+oauth_token+"\""+
                    ", oauth_signature=\""+signature+"\"");
                return webRequest;..
    ...
    can you write pseudo code for upload attachment...
    url, query strings, headers, authorization, should i send a request stream of file content....

    please....:(

    Thanx alot
    QUOTE(ram @ 4 Sep 2012 3:43 PM)
    Can you please explain the problem you are facing?
    0
    • Sep 10, 2012
    Hello,
    Did this code works for you???..... i am struggling please if you have found solution for your issue, please help me......
    help


    QUOTE(Robbie Cohen @ 4 Jan 2011 10:26 PM)
    Hi,
    I'm trying to upload a simple text file using OAuth and UploadAttachment method, but I keep getting a 500 response.
    I have found a single discussion regarding this issue here and followed the example without success.

    Here's my C# code:

    CODE
    string url = @"http://mail.yahooapis.com/ya/upload";

    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
    OAuthUtils utils = new OAuthUtils();

    AuthorizeHeader hdr = utils.GetUserInfoAuthorizationHeader(url, @"yahooapis.com", OAuthConsumerKey, OAuthConsumerSecret, OAuthAccessToken, OAuthTokenSecret, SignatureMethod.HMACSHA1, @"POST");
    byte[] filedata = File.ReadAllBytes(path);
    string base64filecontent = Convert.ToBase64String(filedata);

    req.Headers.Add(@"Authorization", hdr.ToString());
    req.Method = @"POST";

    string boundary = @"---------------abcdefghij";
    req.ContentType = string.Format("multipart/form-data;boundary={0}", boundary);

    StringBuilder content = new System.Text.StringBuilder();
    content.AppendLine(boundary);
    content.AppendLine("Content-Disposition: form-data; name=\"_charset_\"");
    content.AppendLine();
    content.AppendLine(@"utf-8");
    content.AppendLine(boundary);
    content.AppendLine(string.Format("Content-Disposition:form-data; name=\"uploadfile\"; filename=\"{0}\"", path));
    content.AppendLine(@"Content-Type: text/plain");
    content.AppendLine();

    content.AppendLine(base64filecontent);

    content.AppendLine(boundary+@"--");

    byte[] postdata = System.Text.Encoding.UTF8.GetBytes(content.ToString());
    req.ContentLength = postdata.Length;

    Stream s = req.GetRequestStream();
    s.Write(postdata, 0, postdata.Length);

    s.Flush();
    s.Close();
    HttpWebResponse res = (HttpWebResponse)req.GetResponse();


    My authorization header seems to be fine (using same header for other methods without problems).
    Any suggestions would be appreciated :-)
    MK
    0

Recent Posts

in Yahoo! Mail Web Services API