开发者

Integrate Twitter in a website

开发者 https://www.devze.com 2023-01-05 02:11 出处:网络
I am trying to get a website to connect to a Twitter account so that I can display the tweets on my website.I can get the application to work when you connect via the OAuth authentication and it asks

I am trying to get a website to connect to a Twitter account so that I can display the tweets on my website. I can get the application to work when you connect via the OAuth authentication and it asks if I want to allow the application.

What I want to do, is because it's my own Twitter account, I want to be able to login without having to do this every time. I want the website to send my credentials across so that the user just sees the p开发者_Python百科age, and can view the tweets. Is this possible?


If your account is not protected, you can use the public timeline like:

http://twitter.com/statuses/user_timeline/mytwittername.json?callback=twitterCallback2&count=4

this will give you the last 4 tweets, there are other ways to retrieve your tweets whithout authentication


I have figured this out. I created a new class that inherits the IConsumerTokenManager interface. Then, the only properties I needed to change were ConsumerKey, ConsumerSecret and GetTokenSecret (as shown below).

#region Namespaces
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetOpenAuth.OAuth.ChannelElements;
using DotNetOpenAuth.OAuth.Messages; 
#endregion

namespace BingMapTest
{
    public class ConsumerTokenManager : IConsumerTokenManager
    {
        public string ConsumerKey
        {
            get { return "xxxxxxxx"; }
        }

        public string ConsumerSecret
        {
            get { return "xxxxxxxx"; }
        }

        public string GetTokenSecret(string token)
        {
            return "xxxxxxxx";
        }

        public void ExpireRequestTokenAndStoreNewAccessToken(string consumerKey, string requestToken, string accessToken, string accessTokenSecret)
        {
            throw new NotImplementedException();
        }

        public TokenType GetTokenType(string token)
        {
            throw new NotImplementedException();
        }

        public bool IsRequestTokenAuthorized(string requestToken)
        {
            throw new NotImplementedException();
        }

        public void StoreNewRequestToken(UnauthorizedTokenRequest request, ITokenSecretContainingMessage response)
        {
            throw new NotImplementedException();
        }
    }
}

Then, back in my Page_Load method I instantiate the new ConsumerTokenManager:

protected void Page_Load(object sender, EventArgs e)
        {
            ITwitterAuthorization auth;

            ConsumerTokenManager tokenManager = new ConsumerTokenManager();

            auth = new WebOAuthAuthorization(tokenManager, "accessToken");
            auth.UseCompression = true;

            // For Twitter
            using (var twitterCtx = new TwitterContext(auth, "https://api.twitter.com/1/", "https://search.twitter.com/"))
            {                   
                // Whatever authorization module we selected... sign on now.  
                try
                {
                    auth.SignOn();
                }
                catch (OperationCanceledException)
                {
                    return;
                }
            }
        }

Thought I'd share incase anyone else has a similar issue.


You still have to use oauth.

The idea is that when you logged in, any oauth API returns an access token. Sometimes the access token needs to be re-requested periodically, but in Twitter's case the FAQ states (almost at the bottom) that they don't expire them.

This access token is used to sign all future calls - such as retrieving tweets, etc..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号