Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using https://api.twitter.com/1.1/direct_messages.json for retreiving messages for a particular user.
I am able to get 20 messages by default when using this URL but when i want to restrict the output to other than 20 using optional parameter "count" for this API I am getting "The remote server returned an error: (401) Unauthorized." error.
Kindly help me in this scenario.
I am posting my code here.
C#
var oauth_token = "oauth token";
var oauth_token_secret = "oauth token secret";
var oauth_consumer_key = "consumer key";
var oauth_consumer_secret = "consumer secret key";

// oauth implementation details
var oauth_version = "1.0";
var oauth_signature_method = "HMAC-SHA1";

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

// message api details
var status = "Updating status via REST API if this works";
var resource_url = "https://api.twitter.com/1.1/direct_messages.json";
int count = 1;

// create oauth signature
var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&count={6}";
//for default 20 messages
//var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                //"&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&count={6}";

var baseString = string.Format(baseFormat,
                            oauth_consumer_key,
                            oauth_nonce,
                            oauth_signature_method,
                            oauth_timestamp,
                            oauth_token,
                            oauth_version,
                            Uri.EscapeDataString(count.ToString())
                            );

baseString = string.Concat("GET&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString));

var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
                        "&", Uri.EscapeDataString(oauth_token_secret));

string oauth_signature;
using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
{
    oauth_signature = Convert.ToBase64String(
        hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
}

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

var authHeader = string.Format(headerFormat,
                        Uri.EscapeDataString(oauth_nonce),
                        Uri.EscapeDataString(oauth_signature_method),
                        Uri.EscapeDataString(oauth_timestamp),
                        Uri.EscapeDataString(oauth_consumer_key),
                        Uri.EscapeDataString(oauth_token),
                        Uri.EscapeDataString(oauth_signature),
                        Uri.EscapeDataString(oauth_version)
                );

var postBody = "count=" + Uri.EscapeDataString(count.ToString());
resource_url += "?" + postBody;
//for default 20 messages
//var postBody = "count=" + Uri.EscapeDataString(count.ToString());
//resource_url += "?" + postBody;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
request.Headers.Add("Authorization", authHeader);
request.Method = "GET";
request.ContentType = "application/json; charset=utf-8";

WebResponse response = request.GetResponse();
string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();
Posted
Updated 29-May-17 19:44pm
v2
Comments
Member 13686591 21-Feb-18 11:59am    
Hello, since you found the solution to GET request, did you manage to make POST and DELETE requests to work ?

Hi All, Finally I found the solution to my own problem.
Just needed to correct the baseFormat and baseString.

var baseFormat = "count={6}&oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
"&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}";
C#
var baseString = string.Format(baseFormat,
                                       oauth_consumer_key,
                                       oauth_nonce,
                                       oauth_signature_method,
                                       oauth_timestamp,
                                       oauth_token,
                                       oauth_version,
                                       Uri.EscapeDataString(count.ToString())
                                       );


count will be passed prior to all the parameters.
Thanks.
 
Share this answer
 
<pre>  TimelineFormat = "https://api.twitter.com/1.1/direct_messages.json?screen_name="boddupratusha"&include_rts=1&exclude_replies=0&count=5",

I used this Url but unable to read the messages showing
The remote server returned an error: (401)
Plz help me out...
 
Share this answer
 
v4
Comments
maverick1991 10-Aug-17 9:52am    
can u share the exact api url along with the parameters with which you are trying to hit it? then maybe i can help out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900