为什么Yammer发布消息错误500?

时间:2013-10-08 04:15:40

标签: c# .net rest yammer

在发布消息时,Yammer会抛出以下错误(而不是发布消息):

  

远程服务器返回错误:(500)内部服务器错误。

我正在使用Yammer .NET包装器,它可以正常使用REST调用“https://www.yammer.com/api/v1/messages.json”,但发布消息会出现上述错误。

以下是给我们错误消息的代码

String consumerkey = "consumerkey";
String clientsecret = "clientsecret";
WebProxy webpro = new WebProxy("ourproxyserver",8080)
webpro.BypassProxyOnLocal = true;
webpro.Credentials = CredentialCache.DefaultCredentials;
String access_token = "https://www.yammer.com/oauth2/access_token.json"+
                       "?client_id=clientid&"+
                       "client_secret=cli_secret&code=code_id";

Yammer.Session session = new Yammer.Session(
                           new OAuth.OAuthKey(
                                 consumerkey, clientsecret, access_token,"token"
                           ),
                         webpro);

NameValueCollection parameters = new NameValueCollection();
parameters.Add("body", body);

response = Yammer.HttpUtility.Post(Resources.YAMMER_MESSAGES_CREATE, parameters, session);

任何想法可能出错?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下包装方法在yammer上发布消息(请注意,此代码使用AuthAuthbase类:

public static string PostMessage(string body)
{
       NameValueCollection parameters = new NameValueCollection();
       parameters.Add("body", body);
       string response = string.Empty;
       response = Post("https://www.yammer.com/api/v1/messages/", parameters);
       return response;
}

public static string Post(string url, NameValueCollection parameters)
{
            string nonce, timestamp;
            string fullUrl = EncodeUrl(url, parameters);
            string signature = GetSignature(WebMethod.POST, fullUrl, out timestamp, out nonce);
            HttpWebRequest request = CreateWebRequest(url, WebMethod.POST, nonce, timestamp, signature);
int count = 0;
            string queryString = string.Empty;
            foreach (string key in parameters.Keys)
            {
                if (count == 0)
                    queryString = key + "=" + Rfc3986.Encode(parameters[key]);
                else
                    queryString += "&" + key + "=" + Rfc3986.Encode(parameters[key]);
                count++;
            }


            byte[] postDataBytes = Encoding.ASCII.GetBytes(queryString);
            request.ContentLength = postDataBytes.Length;
            Stream reqStream = request.GetRequestStream();
            reqStream.Write(postDataBytes, 0, postDataBytes.Length);
            reqStream.Close();

            WebResponse response = null;
            string data = string.Empty;
            try
            {
                response = request.GetResponse();
                data = response.Headers["Location"];
            }
            catch (Exception ex)
            {
                //System.Windows.Forms.MessageBox.Show("Error retrieving web response " + ex.Message);
                throw ex;
            }
            finally
            {
                if (response != null)
                    response.Close();
            }
            return data;
        }

        private static string EncodeUrl(string url, NameValueCollection parameters)
        {
            string fullUrl = string.Empty;
            int count = 0;
            foreach (string key in parameters.Keys)
            {
                if (count == 0)
                    fullUrl = url + "?" + key + "=" + Rfc3986.Encode(parameters[key].ToLower());
                else
                    fullUrl += "&" + key + "=" + Rfc3986.Encode(parameters[key].ToLower());
                count++;
            }
            return fullUrl;
        }


public static string GetSignature(WebMethod method, string url, out string timestamp, out string nonce)
        {
            OAuthBase oAuth = new OAuthBase();
            nonce = oAuth.GenerateNonce();
            timestamp = oAuth.GenerateTimeStamp();
            string nurl, nrp;

            Uri uri = new Uri(url);
            string sig = oAuth.GenerateSignature(
                uri,
                YOURCONSUMERKEY,
                YOURCONSUMER_SECRET,
                AuthToken,
                AuthTokenSecret,
                method.ToString(),
                timestamp,
                nonce,
                OAuthBase.SignatureTypes.PLAINTEXT, out nurl, out nrp);

            return System.Web.HttpUtility.UrlEncode(sig);
        }

private static HttpWebRequest CreateWebRequest(string fullUrl, WebMethod method, string nonce, string timeStamp, string sig)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fullUrl);
            request.Method = method.ToString();
            request.Proxy = Yammer.Session.WebProxy;
            string authHeader = CreateAuthHeader(method, nonce, timeStamp, sig);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Headers.Add("Authorization", authHeader);
            return request;
        }

private static string CreateAuthHeader(WebMethod method, string nonce, string timeStamp, string sig)
{
            var sb = new StringBuilder();
            sb.Append("OAuth ");
            if (method == WebMethod.POST)
                sb.Append("realm=\"" + "\",");
            else
                sb.Append("realm=\"\",");

            string authHeader = "oauth_consumer_key=\"" + YOURCONSUMERKEY + "\"," +
                                "oauth_token=\"" + YOURAUTHTOKEN  + "\"," +
                                "oauth_nonce=\"" + nonce + "\"," +
                                "oauth_timestamp=\"" + timeStamp + "\"," +
                                "oauth_signature_method=\"" + "HMAC-SHA1" + "\"," +
                                "oauth_version=\"" + "1.0" + "\"," +
                                "oauth_signature=\"" + sig + "\"";

            sb.Append(authHeader);
            return sb.ToString();
}