2016-04-27 14 views
-1

次のコードを使用して、他のIDにメッセージを送信しています。誰かが私がここで何が不足しているか説明してください。両方のアカウントはお互いにフォロワーであり、このオプションは全員からの直接メッセージを受信することも可能です。Twitterで直接メッセージを送るC#

private static void sendMessage(string message) 
{ 
    //The facebook json url to update the status 
    string facebookURL = "https://api.twitter.com/1.1/direct_messages/new.json?user_id=1699575980&screen_name=MuneebZulfee&text=some message"; 

    //set the access tokens (REQUIRED) 
    string oauth_consumer_key = "**************************"; 
    string oauth_consumer_secret = "***************************"; 
    string oauth_token = "******************************"; 
    string oauth_token_secret = "*****************************"; 

    // set the oauth version and signature method 
    string oauth_version = "1.0"; 
    string oauth_signature_method = "HMAC-SHA1"; 

    // create unique request details 
    string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString())); 
    System.TimeSpan timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)); 
    string oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString(); 

    // create oauth signature 
    string baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" + "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&screen_name={6}&text={7}&user_id={8}"; 

    string baseString = string.Format(
     baseFormat, 
     oauth_consumer_key, 
     oauth_nonce, 
     oauth_signature_method, 
     oauth_timestamp, oauth_token, 
     oauth_version, 
     Uri.EscapeDataString("MuneebZulfee"), 
     Uri.EscapeDataString("some message"), 
     Uri.EscapeDataString("1699575980") 
    ); 

    string oauth_signature = null; 
    using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(Uri.EscapeDataString(oauth_consumer_secret) + "&" + Uri.EscapeDataString(oauth_token_secret)))) 
    { 
     oauth_signature = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes("POST&" + Uri.EscapeDataString(facebookURL) + "&" + Uri.EscapeDataString(baseString)))); 
    } 

    // create the request header 
    string authorizationFormat = "OAuth oauth_consumer_key=\"{0}\", oauth_nonce=\"{1}\", " + "oauth_signature=\"{2}\", oauth_signature_method=\"{3}\", " + "oauth_timestamp=\"{4}\", oauth_token=\"{5}\", " + "oauth_version=\"{6}\""; 

    string authorizationHeader = string.Format(
     authorizationFormat, 
     Uri.EscapeDataString(oauth_consumer_key), 
     Uri.EscapeDataString(oauth_nonce), 
     Uri.EscapeDataString(oauth_signature), 
     Uri.EscapeDataString(oauth_signature_method), 
     Uri.EscapeDataString(oauth_timestamp), 
     Uri.EscapeDataString(oauth_token), 
     Uri.EscapeDataString(oauth_version) 
    ); 

    HttpWebRequest objHttpWebRequest = (HttpWebRequest)WebRequest.Create(facebookURL); 
    objHttpWebRequest.Headers.Add("Authorization", authorizationHeader); 
    objHttpWebRequest.Method = "POST"; 
    objHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; 
    using (Stream objStream = objHttpWebRequest.GetRequestStream()) 
    { 
     byte[] content = ASCIIEncoding.ASCII.GetBytes(Uri.EscapeDataString("user_id=1699575980&screen_name=MuneebZulfee&text=some message")); 
     objStream.Write(content, 0, content.Length); 
    } 

    var responseResult = ""; 
    try 
    { 
     //success posting 
     WebResponse objWebResponse = objHttpWebRequest.GetResponse(); 
     StreamReader objStreamReader = new StreamReader(objWebResponse.GetResponseStream()); 
     responseResult = objStreamReader.ReadToEnd().ToString(); 
    } 
    catch (Exception ex) 
    { 
     //throw exception error 
     responseResult = "Twitter Post Error: " + ex.Message.ToString() + ", authHeader: " + authorizationHeader; 
    } 
} 
+0

の場合と

var twitterApp = new TwitterService("******************", "****************"); twitterApp.AuthenticateWith("**********-*********************", "****************"); twitterApp.SendDirectMessage(new SendDirectMessageOptions() { ScreenName = "username", Text = DateTime.UtcNow.Ticks.ToString() }); 

ツイートシャープ

を使用してみてください?あなたはTwitterからエラーメッセージを受け取りますか、または例外がスローされますか? – user1666620

+0

401 UnAuthorized –

+0

Fiddlerなどのツールを使用して、実際にワイヤで何が外に出ているのか確認しましたか?あなたは、Twitterのクライアントを使ってその機能を実行するときに、それをどうやって外に出すのかと比較しましたか? –

答えて

2

これはうまくいかないものを私

+0

ありがとうございました。それはそれをした:) –

+0

ちょうどあなたに知らせるために、TweetSharpはもはや維持されていません。 – Linvi

+0

@Linviこれは本当ですか? twitterが推奨ライブラリとして自分のウェブサイトでこれを言いますので –

関連する問題