0

UploadAttachment 500 error in .NET

I’ve been struggling getting the uploadattachment web service call to succeed.  I can send an email and retrieve contacts so my OAuth implementation seems to be working correct.  Currently I am getting a 500 error when I upload a file. I’m pretty sure the error is with my request but with no working examples available I’m guessing.  Anybody have any ideas or can you point me in the direction of a working .NET example?

Error message: "The remote server returned an error: (500) Internal Server Error." System.InvalidOperationException {System.Net.WebException}

Here’s the latest version my code:
 
String filename = @"c:\temp\text.pdf";
FileInfo afile = new FileInfo(filename);

Uri uri = new Uri("http://mail.yahooapis.com/ya/upload?uploadfile=" + Path.GetFileName(afile.FullName) + "&output=xml");

OAuthBase oauth = new OAuthBase();
string nonce = oauth.GenerateNonce();
string timeStamp = oauth.GenerateTimeStamp();
string normalizedUrl;
string normalizedRequestParameters;

string sig = oauth.GenerateSignature(uri, ConsumerKey, ConsumerSecret, OAuthToken, OAuthTokenSecret, 
                   "POST", timeStamp, nonce, OAuthBase.SignatureTypes.HMACSHA1, 
                   out normalizedUrl, out normalizedRequestParameters);

string authHeader = "Authorization: OAuth " +
 "realm=\"yahooapis.com\"" +
 ",oauth_consumer_key=\"" + ConsumerKey + "\"" +
 ",oauth_nonce=\"" + nonce + "\"" +
 ",oauth_signature_method=\"HMAC-SHA1\"" +
 ",oauth_timestamp=\"" + timeStamp + "\"" +
 ",oauth_token=\"" + OAuthToken + "\"" +
 ",oauth_version=\"1.0\"" +
 ",oauth_signature=\"" + HttpUtility.UrlEncode(sig) + "\""; 

// Load the pdf file
byte[] data = File.ReadAllBytes(filename);

// Create the HTTPWebRequest and set the parameters
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(uri);
httpReq.Method = "POST";
httpReq.ContentType = "binary/octet-stream ";
httpReq.ContentLength = data.Length;
httpReq.Headers.Add(authHeader);

Stream requestStream = httpReq.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();

HttpWebResponse response = (HttpWebResponse)httpReq.GetResponse();

Stream respStrm = response.GetResponseStream();
StreamReader stream = new StreamReader(respStrm);
string resultString = stream.ReadToEnd();
<process the result>
Thanks!
Cody

by
2 Replies
  • I did get this to work and wanted to follow up with my working code in case it helps someone down the road.  The big change was to switch from using raw HTTPWebRequest object and use the WebClient object instead.  What that worked I’m not sure but it does.  I’m assuming it’s something the WebClient sets in the request that I was not doing in the old code.

     

      String filename = @"c:\temp\text.pdf";
      FileInfo afile = new FileInfo(filename);
    
      Uri uri = new Uri("http://mail.yahooapis.com/ya/upload?uploadfile=" + Path.GetFileName(afile.FullName) + "&output=xml");
    
      OAuthBase oauth = new OAuthBase();
      string nonce = oauth.GenerateNonce();
      string timeStamp = oauth.GenerateTimeStamp();
      string normalizedUrl;
      string normalizedRequestParameters;
      string sig = oauth.GenerateSignature(uri, ConsumerKey, ConsumerSecret, OAuthToken, OAuthTokenSecret, httpmethod, timeStamp, nonce, signaturetype, out normalizedUrl, out normalizedRequestParameters);
    
      string authHeader "Authorization: OAuth " +
      "realm=\"yahooapis.com\"" +
      ",oauth_consumer_key=\"" + ConsumerKey + "\"" +
      ",oauth_nonce=\"" + nonce + "\"" +
      ",oauth_signature_method=\"HMAC-SHA1\"" +
      ",oauth_timestamp=\"" + timeStamp + "\"" +
      ",oauth_token=\"" + OAuthToken + "\"" +
      ",oauth_version=\"1.0\"" +
      ",oauth_signature=\"" + HttpUtility.UrlEncode(sig) + "\"";
      
      WebClient uploadClient = new WebClient();
      uploadClient.Headers.Add(authHeader);
      string response = new System.Text.ASCIIEncoding().GetString(uploadClient.UploadFile(uri, filename));
      <process the result>
    

    0
  • Thanks Cody.
    0

Recent Posts

in Yahoo! Mail Web Services API