Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C Windows窗体在Twitter上发布状态更新_C#_Windows_Winforms_Twitter_Status - Fatal编程技术网

C# C Windows窗体在Twitter上发布状态更新

C# C Windows窗体在Twitter上发布状态更新,c#,windows,winforms,twitter,status,C#,Windows,Winforms,Twitter,Status,C Windows窗体在Twitter上发布状态更新 我已经尝试了我能找到的所有解决方案,并拼凑了以下代码,它总是给出401授权错误 当我在twitter中设置一个twitter应用程序时,它要求一个回调url,我正在本地机器上开发这个应用程序,需要使用windows窗体,以便tweet可以从本地数据库获取数据。我只想从我自己的账户上发推,但当我添加新项目时,从我的数据库中获取数据插入推 我已经尝试将编码改为UTF8,之前的字符串定义被注释掉,以显示在哪里,阅读后,这可能是某个地方的问题 我不知

C Windows窗体在Twitter上发布状态更新

我已经尝试了我能找到的所有解决方案,并拼凑了以下代码,它总是给出401授权错误

当我在twitter中设置一个twitter应用程序时,它要求一个回调url,我正在本地机器上开发这个应用程序,需要使用windows窗体,以便tweet可以从本地数据库获取数据。我只想从我自己的账户上发推,但当我添加新项目时,从我的数据库中获取数据插入推

我已经尝试将编码改为UTF8,之前的字符串定义被注释掉,以显示在哪里,阅读后,这可能是某个地方的问题

我不知道授权是否有问题,因为我拥有所有必需的密钥,或者是否与回调url有关,如果是,对于没有网站的windows窗体客户端,我可以使用什么

如果我将dev.twitter站点中的CURL粘贴到浏览器中,则在使用它时会得到错误的身份验证数据,默认值为1,并给出一条消息,说明这已被弃用。我为回调url输入的网站是我的,但我不知道是否需要有代码才能运行此测试

如果有任何帮助,我将不胜感激。我已经努力解决这个问题一周了

public static void anotherTweet(string status)
{
    //GS - Get the oAuth params
    //string status = status;
    string postBody = "status=" + 
        Uri.EscapeDataString(status);

    string oauth_consumer_key = "xxx";
    //string oauth_nonce = Convert.ToBase64String(
        //new ASCIIEncoding().GetBytes(
           // DateTime.Now.Ticks.ToString()));
    string oauth_nonce = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(DateTime.Now.Ticks.ToString()));    
    string oauth_signature_method = "HMAC-SHA1";
    string oauth_token =
        "xxx";
    string oauth_version = "1.0";
    TimeSpan ts = DateTime.UtcNow - 
        new DateTime(1970, 1, 1, 0, 0, 0, 0);

    string oauth_timestamp = 
        Convert.ToInt64(ts.TotalSeconds).ToString();

    //string oauth_version = "1.0";

    //GS - When building the signature string the params
    //must be in alphabetical order. I can't be bothered
    //with that, get SortedDictionary to do it's thing
    SortedDictionary<string, string> sd = 
        new SortedDictionary<string, string>();

    sd.Add("status", status);
    sd.Add("oauth_version", oauth_version);
    sd.Add("oauth_consumer_key", oauth_consumer_key);
    sd.Add("oauth_nonce", oauth_nonce);
    sd.Add("oauth_signature_method", oauth_signature_method);
    sd.Add("oauth_timestamp", oauth_timestamp);
    sd.Add("oauth_token", oauth_token);

    //GS - Build the signature string
    string baseString = String.Empty;
    baseString += "POST" + "&";

    baseString += Uri.EscapeDataString(
        "https://api.twitter.com/1.1/statuses/update.json") 
        + "&";

    foreach (KeyValuePair<string,string> entry in sd)
    {
        baseString += Uri.EscapeDataString(entry.Key + 
            "=" + entry.Value + "&");
    }

    //GS - Remove the trailing ambersand char, remember 
    //it's been urlEncoded so you have to remove the 
    //last 3 chars - %26
    baseString = 
        baseString.Substring(0, baseString.Length - 3);

    //GS - Build the signing key
    string consumerSecret =
        "xxx";

    string oauth_token_secret =
        "xxx";

    string signingKey = 
        Uri.EscapeDataString(consumerSecret) + "&" + 
        Uri.EscapeDataString(oauth_token_secret);

    //GS - Sign the request
    HMACSHA1 hasher = new HMACSHA1(
        new ASCIIEncoding().GetBytes(signingKey));

    //string signatureString = Convert.ToBase64String(
        //hasher.ComputeHash(
       // new ASCIIEncoding().GetBytes(baseString)));

    string signatureString = Convert.ToBase64String(
        hasher.ComputeHash(
    System.Text.Encoding.UTF8.GetBytes(baseString)));


    //GS - Tell Twitter we don't do the 100 continue thing
    ServicePointManager.Expect100Continue = false;

    //GS - Instantiate a web request and populate the 
    //authorization header
    HttpWebRequest hwr = 
        (HttpWebRequest)WebRequest.Create(
        @"https://api.twitter.com/1.1/statuses/update.json");

    string authorizationHeaderParams = String.Empty;
    authorizationHeaderParams += "OAuth ";
    authorizationHeaderParams += "oauth_nonce=" + "\"" + 
        Uri.EscapeDataString(oauth_nonce) + "\",";

    authorizationHeaderParams += 
        "oauth_signature_method=" + "\"" + 
        Uri.EscapeDataString(oauth_signature_method) + 
        "\",";

    authorizationHeaderParams += "oauth_timestamp=" + "\"" + 
        Uri.EscapeDataString(oauth_timestamp) + "\",";

    authorizationHeaderParams += "oauth_consumer_key=" 
        + "\"" + Uri.EscapeDataString(
        oauth_consumer_key) + "\",";

    authorizationHeaderParams += "oauth_token=" + "\"" + 
        Uri.EscapeDataString(oauth_token) + "\",";

    authorizationHeaderParams += "oauth_signature=" + "\"" 
        + Uri.EscapeDataString(signatureString) + "\",";

    //authorizationHeaderParams += "oauth_version=" + "\"" + 
        //Uri.EscapeDataString(oauth_version) + "\"";

    hwr.Headers.Add(
        "Authorization", authorizationHeaderParams);

    //GS - POST off the request
    hwr.Method = "POST";
    hwr.ContentType = "application/x-www-form-urlencoded";
    Stream stream = hwr.GetRequestStream();
    //byte[] bodyBytes = 
        //new ASCIIEncoding().GetBytes(postBody);
    byte[] bodyBytes = System.Text.Encoding.UTF8.GetBytes(postBody);    
    stream.Write(bodyBytes, 0, bodyBytes.Length);
    stream.Flush();
    stream.Close();

    //GS - Allow us a reasonable timeout in case
    //Twitter's busy
    hwr.Timeout = 3 * 60 * 1000;

    try 
    { 
        HttpWebResponse rsp = hwr.GetResponse() 
            as HttpWebResponse;
        //GS - Do something with the return here...
    }
    catch (WebException e)
    {
        MessageBox.Show(e.Message);
    }
} 

如果您只想从您的帐户发布,只需在dev.twitter.com上创建一个令牌即可。 然后,您可以直接在代码中使用新应用程序的凭据


与前面的评论一样,我建议使用OAuth或Twitter库。

OAuth可能很难排除故障。我强烈建议您使用现有的OAuth库之一,而不是使用您自己的OAuth代码。这将使您能够专注于应用程序,而不是调试OAuth。我会在