android 腾讯微博开发 OAuth认证(一):获取请求指令RequeToken

本文详细介绍了如何在Android项目中集成腾讯微博SDK,包括下载SDK、配置项目、编写代码实现OAuth1认证等步骤,并提供了完整的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:下载腾讯微博SDK

第二步:创建Android项目,并配置Build Path,加入SDK中httpmime-4.1.3.jar、Android_SDK_v1.2.jar的源文件(里面包含错误需要自己进行相关的修改)两个库文件

 

相关截图:

 

 

源代码:

package com.rf.qqweixun;

import com.tencent.weibo.oauthv1.OAuthV1;
import com.tencent.weibo.oauthv1.OAuthV1Client;
import com.tencent.weibo.utils.QHttpClient;
import com.tencent.weibo.webview.OAuthV1AuthorizeWebView;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {
	// 相关组件
	private Button start;
	private Button webview;
	private TextView content;
	private Intent intent;
	// 相关OAuth资源http://www.tencent.com/zh-cn/index.shtml||http://blog.youkuaiyun.com/zhouzhiwengang
	// !!!请根据您的实际情况修改!!! 认证成功后浏览器会被重定向到这个url中 本例子中不需改动
	private String oauthCallback = "http://write.blog.youkuaiyun.com/postlist";
	// !!!请根据您的实际情况修改!!! 换为您为自己的应用申请到的APP KEY
	private String oauthConsumeKey = "801405166";
	// !!!请根据您的实际情况修改!!! 换为您为自己的应用申请到的APP SECRET
	private String oauthConsumerSecret = "2a2577742611abe65329e513ae3e2281";
	private OAuthV1 oAuth; // Oauth鉴权所需及所得信息的封装存储单元

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		start = (Button) findViewById(R.id.start);
		content = (TextView) findViewById(R.id.content);
		// OAuth1认证初始化
		oAuth = new OAuthV1(oauthCallback);
		oAuth.setOauthConsumerKey(oauthConsumeKey);
		oAuth.setOauthConsumerSecret(oauthConsumerSecret);

		// 关闭OAuthV1Client中的默认开启的QHttpClient。
		OAuthV1Client.getQHttpClient().shutdownConnection();
		// 为OAuthV1Client配置自己定义QHttpClient。
		OAuthV1Client.setQHttpClient(new QHttpClient());

		start.setOnClickListener(new OnClickListener() {
			// 应用获取未授权的Request Token
			@Override
			public void onClick(View v) {
				// 向腾讯微博开放平台请求获得未授权的Request_Token
				try {

					oAuth = OAuthV1Client.requestToken(oAuth);
					String oauthToken = oAuth.getOauthToken();
					String oauthToken_Secret = oAuth.getOauthTokenSecret();
					System.out.println("Token的值:" + oauthToken);
					System.out.println("Token_Secret的值:" + oauthToken_Secret);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.out.println("相关信息:" + e.getMessage());
				}
				// 未授权的Request Token
				// Toast.makeText(MainActivity.this,
				// "未授权的Request_Token:"+oAuth.getOauthToken()+"\n"+oAuth.getOauthTokenSecret(),
				// Toast.LENGTH_LONG).show();
				content.setText("未授权的Request_Token:" + oAuth.getOauthToken()
						+ "\n" + oAuth.getOauthTokenSecret());
			}

		});

		webview = (Button) findViewById(R.id.webview);
		webview.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				intent = new Intent(MainActivity.this,
						OAuthV1AuthorizeWebView.class);// 创建Intent,使用WebView让用户授权
				intent.putExtra("oauth", oAuth);
				startActivityForResult(intent, 1);

			}

		});

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1) {
			if (resultCode == OAuthV1AuthorizeWebView.RESULT_CODE) {
				// 从返回的Intent中获取验证码
				oAuth = (OAuthV1) data.getExtras().getSerializable("oauth");
				// 用户授权Request Token
				Toast.makeText(MainActivity.this,
						"用户授权的Request_Token:" + oAuth.getOauthVerifier(),
						Toast.LENGTH_LONG).show();

			}
		}
	}

}



配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rf.qqweixun"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="10" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.rf.qqweixun.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.tencent.weibo.webview.OAuthV1AuthorizeWebView" >
        </activity>
    </application>
    <!-- 应用程序的相关权限 -->
    <uses-permission android:name="android.permission.INTERNET" />

</manifest>

 

修改SDK_1.2源代码:[com.tencent.weibo.utils.QHttpClient]

   public String httpGet(final String url, final String queryString) throws Exception {

       new Thread(){

		@Override
		public void run() {
			// TODO Auto-generated method stub
//			  if (queryString != null && !queryString.equals("")) {
//		            url=url +"?" + queryString;
//		        }
		        Log.i(TAG, "QHttpClient httpGet [1] url = "+url);

		        HttpGet httpGet = new HttpGet(url +"?" + queryString);
		        httpGet.getParams().setParameter("http.socket.timeout",
		                new Integer(CONNECTION_TIMEOUT));
		        try {
		            HttpResponse response=httpClient.execute(httpGet);
		            Log.i(TAG, "QHttpClient httpGet [2] StatusLine : "+ response.getStatusLine());
		            responseData =EntityUtils.toString(response.getEntity());
		            Log.i(TAG, "QHttpClient httpGet [3] Response = "+ responseData);
		        } catch (Exception e) {
		            e.printStackTrace();
		        } finally {
		            httpGet.abort();
		        }

		}
    	   
    	   
       }.start();
      
        return responseData;
    }




 

效果截图:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值