2

My goal is to get all the retweet IDs of a specific tweet. I tried to use oAuthTwitterWrapper for C# twitter authentication and tried changing the code, but without any success. I am getting Forbidden message from twitter when I change the searchFormat to suit my requirement. Someone please help!

oAuthTwitterWrapper Wrapper - https://github.com/andyhutch77/oAuthTwitterWrapper Stack Exchange Link - Authenticate and request a user's timeline with Twitter API 1.1 oAuth

Thanks in advance..

Community
  • 1
  • 1
Vyas Rao
  • 478
  • 6
  • 10
  • 25
  • Unfortunately I do not think that the search api contains the information. https://dev.twitter.com/docs/api/1.1/get/search/tweets – hutchonoid Jan 23 '14 at 09:26
  • Someone added the search facility but it is included in the demo web app and web form app here: https://github.com/andyhutch77/oAuthTwitterWrapper If you run that to begin with it should work as is without a 404, if you provide your secret key info. Take a look at the json coming back in the debugger. It only seems to contain retweet_count and retweeted fields. – hutchonoid Jan 23 '14 at 09:28
  • Is there a way to make use of https://api.twitter.com/1/statuses/retweets/ ? – Vyas Rao Jan 23 '14 at 09:28

1 Answers1

1

Ok, I think this is how you would do this manually to begin with.

I have not tested it so there may be some typos, let me know and I will update it the answer accordingly.

This makes a call to the api specified here:

https://dev.twitter.com/docs/api/1.1/get/statuses/retweets/%3Aid

// You need to set your own keys and tweet id
var oAuthConsumerKey = "superSecretKey";
var oAuthConsumerSecret = "superSecretSecret";
var oAuthUrl = "https://api.twitter.com/oauth2/token";
var tweetId = "21947795900469248";

// Do the Authenticate
var authHeaderFormat = "Basic {0}";

var authHeader = string.Format(authHeaderFormat,
    Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(oAuthConsumerKey) + ":" +
    Uri.EscapeDataString((oAuthConsumerSecret)))
));

var postBody = "grant_type=client_credentials";

HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);
authRequest.Headers.Add("Authorization", authHeader);
authRequest.Method = "POST";
authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

using (Stream stream = authRequest.GetRequestStream())
{
    byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
    stream.Write(content, 0, content.Length);
}

authRequest.Headers.Add("Accept-Encoding", "gzip");

WebResponse authResponse = authRequest.GetResponse();
// deserialize into an object
TwitAuthenticateResponse twitAuthResponse;
using (authResponse)
{
    using (var reader = new StreamReader(authResponse.GetResponseStream())) {
        JavaScriptSerializer js = new JavaScriptSerializer();
        var objectText = reader.ReadToEnd();
        twitAuthResponse = JsonConvert.DeserializeObject<TwitAuthenticateResponse>(objectText);
    }
}

// Get the retweets by Id
var retweetFormat = "https://api.twitter.com/1.1/statuses/retweets/{0}.json";
var retweetsUrl = string.Format(retweetFormat, tweetId);
HttpWebRequest retweetRequest = (HttpWebRequest)WebRequest.Create(retweetsUrl);
var retweetHeaderFormat = "{0} {1}";
timeLineRequest.Headers.Add("Authorization", string.Format(retweetHeaderFormat, twitAuthResponse.token_type, 

twitAuthResponse.access_token));
retweetRequest.Method = "Get";
WebResponse retweetResponse = timeLineRequest.GetResponse();
var retweetJson = string.Empty;
using (retweetResponse)
{
    using (var reader = new StreamReader(retweetResponse.GetResponseStream()))
    {
         retweetJson = reader.ReadToEnd();
    }
}

//parse the json from retweetJson to read the returned id's


public class TwitAuthenticateResponse {
    public string token_type { get; set; }
    public string access_token { get; set; }
}

If this works and you have time please submit a pull request via GitHub and I will include it in oauthtwitterwrapper.

hutchonoid
  • 32,982
  • 15
  • 99
  • 104