为什么 Yammer 发布消息错误 500

本文关键字:消息 错误 Yammer 布消息 为什么 | 更新日期: 2023-09-27 18:34:15

发布消息时,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);

任何想法可能出了什么问题?

为什么 Yammer 发布消息错误 500

您可以尝试以下包装器方法在 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();
}