I am doing a small application on ASP . NET with Twitter API. One of my tasks is to get my friend list. The way I am doing this is I send a request from a Javscript file inside a Web Application to a controller in another project that is a Web API. Then, from the Web API, I send the GET request to Twitter API. Now, I have already done this with my Twitter Feed and Favorite tweets,and it worked perfectly, however, I cannot achieve this with my friends list…I am using literally the same code and getting error 500.

This is the code inside the javascript:

function getMyFriends() {
    $.getJSON("myLocalHostLink/api/Twitter/GetFriends?accessToken=" + 
      sessionStorage.getItem("twitterToken") + "&accessTokenSecret=" + 
      sessionStorage.getItem("twitterSecretToken"))
        .done(function (data) {
            $.each(data, function (key, item) {
               console.log(item)
            });
        });
}

This is the code inside my controller:

[System.Web.Http.Route("api/Twitter/GetFriends")]
        public IEnumerable<ITweet> GetFriends2(string accessToken, string accessTokenSecret)
        {
            var userCredentials = Auth.SetUserCredentials("myKey", 
     "mySecretKey", accessToken, 
     accessTokenSecret);
            var authenticatedUser = Tweetinvi.User.GetAuthenticatedUser(userCredentials);

            var homeTimeline = authenticatedUser.GetFriends(2);
            var json = Tweetinvi.JsonSerializer.ToJson(homeTimeline);

            var tweetDTOs = Tweetinvi.JsonSerializer.ConvertJsonTo<ITweetDTO[]>(json);
            var tweets = Tweet.GenerateTweetsFromDTO(tweetDTOs);

            return tweets;
        }

Thanks

If the javascript is on the client, it’s not secure to send Access tokens and secret as url parameters to your API - those should be handled server side, treating them as you would a password https://developer.twitter.com/en/docs/basics/authentication/guides/authentication-best-practices

What’s the exact error code returned by twitter? https://developer.twitter.com/en/docs/basics/response-codes not the http error 500, is there a code sent back? If it keeps happening, try to reduce the size of the call - eg, if count=5000 make it count=2500 for friends/ids or something like that.

This is the example for getting friends https://github.com/linvi/tweetinvi/wiki/Get-All-Friends-Code but i don’t know C# well enough to see what’s wrong with your example.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.