android 腾讯微博开发 OAuth认证(一):获取请求指令RequeToken
java哥
阅读:623
2021-04-01 10:19:25
评论:0
第一步:下载腾讯微博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.csdn.net/zhouzhiwengang
// !!!请根据您的实际情况修改!!! 认证成功后浏览器会被重定向到这个url中 本例子中不需改动
private String oauthCallback = "http://write.blog.csdn.net/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;
}
效果截图:
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。