Twitter OAuth 1.0

OAuth 2.0 比OAuth 1.0简洁多了,大多数的公司都已经支持OAuth 2.0。但Twiter目前还是不支持OAuth 2.0。没办法,只能用OAuth 1.0实现了。在做的过程中,csdn上的有篇文章对我dt很大,感谢写这篇博文的博主(http://blog.csdn.net/yangjian8915/article/details/11816669

具体的实现步骤如下:

Step 1 , 在Twitter上注册自己的第三方应用。

注册地址:https://apps.twitter.com/app/new

填写回调地址,如:http://10.124.10.56:8080/callback.do


注册成功后,可以找到生成的API Key , 和 API Secrect。这两个参数很重要。后面的步骤会用上。


Step2 ,使用https://api.twitter.com/oauth/request_token获取未授权的token

组装请求 未授权的token 的URL

public static String getRequestTokenUrl()
	{
		// this particular request uses POST
		//String get_or_post = "GET";

		// I think this is the signature method used for all Twitter API calls
		String oauth_signature_method = "HMAC-SHA1";

		// generate any fairly random alphanumeric string as the "nonce". Nonce = Number used ONCE.
		String uuid_string = UUID.randomUUID().toString();
		uuid_string = uuid_string.replaceAll("-", "");
		String oauth_nonce = uuid_string; // any relatively random alphanumeric string will work here

		// get the timestamp
		Calendar tempcal = Calendar.getInstance();
		long ts = tempcal.getTimeInMillis();// get current time in milliseconds
		String oauth_timestamp = (new Long(ts/1000)).toString(); // then divide by 1000 to get seconds

		// assemble the proper parameter string, which must be in alphabetical order, using your consumer key
		String parameter_string = "oauth_consumer_key=" + twitter_consumer_key + "&oauth_nonce=" + oauth_nonce + "&oauth_signature_method=" + oauth_signature_method + "&oauth_timestamp=" + oauth_timestamp + "&oauth_version="+twitter_oauth_version;		
		//System.out.println("parameter_string=" + parameter_string); // print out parameter string for error checking, if you want

		// specify the proper twitter API endpoint at which to direct this request
		String twitter_endpoint = "https://api.twitter.com/oauth/request_token";

		// assemble the string to be signed. It is METHOD & percent-encoded endpoint & percent-encoded parameter string
		// Java's native URLEncoder.encode function will not work. It is the wrong RFC specification (which does "+" where "%20" should be)... 
		// the encode() function included in this class compensates to conform to RFC 3986 (which twitter requires)
		String signature_base_string = get_or_post + "&"+ encode(twitter_endpoint) + "&" + encode(parameter_string);

		// now that we've got the string we want to sign (see directly above) HmacSHA1 hash it against the consumer secret
		String oauth_signature = "";
		oauth_signature = computeSignature(signature_base_string, twitter_consumer_secret + "&");  // note the & at the end. Normally the user access_token would go here, but we don't know it yet for request_token

		String getOauth_tokenUrl = getOauthTokenUrl(twitter_endpoint,null,twitter_consumer_key, oauth_nonce,
				oauth_signature,oauth_signature_method, oauth_timestamp,twitter_oauth_version);
		System.out.println("getOauth_tokenUrl = " + getOauth_tokenUrl);
		return getOauth_tokenUrl;
	}
<pre name="code" class="java">public static String encode(String value) 
	{
        String encoded = null;
        try {
            encoded = URLEncoder.encode(value, "UTF-8");
        } catch (UnsupportedEncodingException ignore) {
        }
        StringBuilder buf = new StringBuilder(encoded.length());
        char focus;
        for (int i = 0; i < encoded.length(); i++) {
            focus = encoded.charAt(i);
            if (focus == '*') {
                buf.append("%2A");
            } else if (focus == '+') {
                buf.append("%20");
            } else if (focus == '%' && (i + 1) < encoded.length()
                    && encoded.charAt(i + 1) == '7' && encoded.charAt(i + 2) == 'E') {
                buf.append('~');
                i += 2;
            } else {
                buf.append(focus);
            }
        }
        return buf.toString();
    }

	private static String computeSignature(String baseString, String keyString)
	{
		try
		{
			SecretKey secretKey = null;

		    byte[] keyBytes = keyString.getBytes();
		    secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");

		    Mac mac = Mac.getInstance("HmacSHA1");
		    mac.init(secretKey);

		    byte[] text = baseString.getBytes();

		    return new String(Base64.encodeBase64(mac.doFinal(text))).trim();
		}
		catch(Exception e)
		{
			return null;
		}
	    
	}


 发送这个请求后,Twitter认证服务器会响应如下格式 
oauth_token=ev7WfVJVvPTahgivLYzwiOqbcrBJVqQUh8YJ7Yr4Jug&oauth_token_secret=nqKT8dtI9DTW208NiMEu2RDQCG6I6gshzBzmyuXjSo&oauth_callback_confirmed=true

step3 ,使用http://api.twitter.com/oauth/authorize进行确认

利用上一步得到的oauth_token组装URL

public static String getOauthVerifierUrl(String oauth_token, String oauth_token_secret)
	{
		// this particular request uses POST
		//String get_or_post = "GET";

		// I think this is the signature method used for all Twitter API calls
		String oauth_signature_method = "HMAC-SHA1";

		// generate any fairly random alphanumeric string as the "nonce". Nonce = Number used ONCE.
		String uuid_string = UUID.randomUUID().toString();
		uuid_string = uuid_string.replaceAll("-", "");
		String oauth_nonce = uuid_string; // any relatively random alphanumeric string will work here

		// get the timestamp
		Calendar tempcal = Calendar.getInstance();
		long ts = tempcal.getTimeInMillis();// get current time in milliseconds
		String oauth_timestamp = (new Long(ts/1000)).toString(); // then divide by 1000 to get seconds

		// assemble the proper parameter string, which must be in alphabetical order, using your consumer key
		String parameter_string = "oauth_consumer_key=" + twitter_consumer_key + "&oauth_nonce=" + oauth_nonce + "&oauth_signature_method=" + oauth_signature_method + "&oauth_timestamp=" + oauth_timestamp + "&oauth_token=" + oauth_token +"&oauth_version="+twitter_oauth_version;		
		//System.out.println("parameter_string=" + parameter_string); // print out parameter string for error checking, if you want

		// specify the proper twitter API endpoint at which to direct this request
		String twitter_endpoint = "https://api.twitter.com/oauth/authorize";

		// assemble the string to be signed. It is METHOD & percent-encoded endpoint & percent-encoded parameter string
		// Java's native URLEncoder.encode function will not work. It is the wrong RFC specification (which does "+" where "%20" should be)... 
		// the encode() function included in this class compensates to conform to RFC 3986 (which twitter requires)
		String signature_base_string = get_or_post + "&"+ encode(twitter_endpoint) + "&" + encode(parameter_string);
		//TODO set the key : twitter_consumer_secret + "&" + oauth_token_secret
		String oauth_signature = computeSignature(signature_base_string, twitter_consumer_secret + "&");  // note the & at the end. Normally the user access_token would go here, but we don't know it yet for request_token
		String getOauth_tokenUrl = getOauthTokenUrl(twitter_endpoint,null,twitter_consumer_key, oauth_nonce,
				oauth_signature,oauth_signature_method, oauth_timestamp,oauth_token,twitter_oauth_version);
		System.out.println("getOauth_tokenUrl = " + getOauth_tokenUrl);
		return getOauth_tokenUrl;
	}

验证成功后,会重写向到回调地址(如:http://10.124.10.56:8080/callback.do)
获取返回的参数,oauth_token,oauth_verifier 
String oauth_token = req.getParameter("oauth_token");
String oauth_verifier = req.getParameter("oauth_verifier");

step4 ,获取access_token

利用上一步返回的两个参数组装URL
public static String getAccessTokenUrl(String oauth_token, String oauth_verifier)
	{
		// this particular request uses POST
		//String get_or_post = "GET";

		// I think this is the signature method used for all Twitter API calls
		String oauth_signature_method = "HMAC-SHA1";

		// generate any fairly random alphanumeric string as the "nonce". Nonce = Number used ONCE.
		String uuid_string = UUID.randomUUID().toString();
		uuid_string = uuid_string.replaceAll("-", "");
		String oauth_nonce = uuid_string; // any relatively random alphanumeric string will work here

		// get the timestamp
		Calendar tempcal = Calendar.getInstance();
		long ts = tempcal.getTimeInMillis();// get current time in milliseconds
		String oauth_timestamp = (new Long(ts/1000)).toString(); // then divide by 1000 to get seconds

		// assemble the proper parameter string, which must be in alphabetical order, using your consumer key
		String parameter_string = "oauth_consumer_key=" + twitter_consumer_key + "&oauth_nonce=" + oauth_nonce 
				+ "&oauth_signature_method=" + oauth_signature_method + "&oauth_timestamp=" + oauth_timestamp 
				+ "&oauth_token=" + oauth_token + "&oauth_verifier=" + oauth_verifier +"&oauth_version="+twitter_oauth_version;		
		//System.out.println("parameter_string=" + parameter_string); // print out parameter string for error checking, if you want

		// specify the proper twitter API endpoint at which to direct this request
		String twitter_endpoint = "https://api.twitter.com/oauth/access_token";

		// assemble the string to be signed. It is METHOD & percent-encoded endpoint & percent-encoded parameter string
		// Java's native URLEncoder.encode function will not work. It is the wrong RFC specification (which does "+" where "%20" should be)... 
		// the encode() function included in this class compensates to conform to RFC 3986 (which twitter requires)
		String signature_base_string = get_or_post + "&"+ encode(twitter_endpoint) + "&" + encode(parameter_string);
		//TODO twitter_consumer_secret + "&" + oauth_token_secret
		String oauth_signature = computeSignature(signature_base_string, twitter_consumer_secret + "&");  // note the & at the end. Normally the user access_token would go here, but we don't know it yet for request_token
		String getOauth_tokenUrl = getOauthTokenUrl(twitter_endpoint,null,twitter_consumer_key, oauth_nonce,
				oauth_signature,oauth_signature_method, oauth_timestamp,oauth_token,oauth_verifier,twitter_oauth_version);
		System.out.println("getOauth_tokenUrl = " + getOauth_tokenUrl);
		return getOauth_tokenUrl;
	}

请求该URL后,就可以得到如下的返回值
oauth_token=2511929677-XEj10zdvnzAArJI9eOHgqVxtqHe5TJH4G9AmczX&oauth_token_secret=h6EYRq8RUHLrJhwizBT2p7wj18WYe61MD5f9jPcDq9aFj&user_id=2511929677&screen_name=KeithWang3
oauth_token就是我们需要的access_token,值得注意的是oauth_token_secret也是很重要的,访问twitter的API,需要通过这个oauth_token_secret生成对应的auth_signature。


附件有更新,新增了以前没有实现的tweet功能,这里附件不能更新,请有兴趣的,到这里去下载:http://code.google.com/p/gridtwit/ ---------------------------------------------------------------------- you can refer to the demo program "gridtwit": http://code.google.com/p/gridtwit/ especially the files: "gridtwit_client_oauth_demo\Demo_Description.doc" and "gridtwit_client_oauth_demo\liboauth-0.9.1\tests\gridtwit.c" It details oauth authentication flow and coding(c++, liboauth). ----------------------------------------------------------- Version info V0.1.0 20100915 oauth authentication flow demo request token direct token access token timeline get get public timeline get friend timeline get following timeline get user timeline ids get get user's followed friends' ids (followers' ids) get user's following friends' id tweet get get tweet by id V0.1.1 20100923 tweet send the function works ok. upgrade liboauth liboauth-0.9.1 <- liboauth-0.8.9 Prompt: The latest version information in the file gridtwit.c recorded in the third line. ------------------------------------------------------------------------------- gridtwit_client_oauth_demo_src_0.1.0 用 C++ 语言实现 twitteroauth 鉴权全过程。另外还有详细的文档试用说明(Demo_Description.doc)。这里拿出来共享,是希望大家在做同类产品时,在 OAUTH 鉴权过程中,可以参考这个,并少花点时间。 文件清单: gridtwit_client_oauth_demo.tar.gz file list: | Demo_Description.doc | gridtwit_run_output_info.txt | \---liboauth-0.8.9 | aclocal.m4 | AUTHORS | ChangeLog | compile | config.guess | config.sub | configure | configure.ac | COPYING | COPYING.GPL | COPYING.MIT | depcomp | Doxyfile.in | INSTALL | install-sh | liboauth.lsm.in | LICENSE.OpenSSL | ltmain.sh | Makefile.am | Makefile.in | missing | NEWS | oauth.pc.in | README | +---doc | libOAuth.png | mainpage.dox | mainpage.dox.in | Makefile.am | Makefile.in | oauth.3 | +---m4 | libtool.m4 | ltoptions.m4 | ltsugar.m4 | ltversion.m4 | lt~obsolete.m4 | +---src | config.h | config.h.in | hash.c | Makefile.am | Makefile.in | oauth.c | oauth.h | oauth_http.c | xmalloc.c | xmalloc.h | \---tests commontest.c commontest.h gridtwit.c ---- add Makefile.am ---- Modified, please cover the original file Makefile.in ---- Modified, please cover the original file oauthbodyhash.c oauthdatapost.c oauthexample.c oauthsign.c oauthtest.c oauthtest2.c selftest_eran.c selftest_other.c selftest_wiki.c
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值