如果大家開發(fā)過Android版的新浪微博客戶端可以通過如下接口直接在客戶端返回oauth_verifier
以下是我加的方法,注意增加了userId和passwd參數(shù),大小寫userId的I是大寫,該問題困擾了我1晚上。
l
public String getAuthorizationVerifier(String uid,String pass) {
l
return httpClient.getAuthorizationURL() + "?oauth_token=" +
getToken()+"&userId="+uid+"&passwd="+pass+"&oauth_callback=json";
l
l
}
但是在騰訊中沒有該方法,只能彈出頁(yè)面,之后就跳轉(zhuǎn)到了另一個(gè)url,而oauth_verifier在url中,不能通過代碼直接獲取,其實(shí)在Android中式可以解決的,如下圖:
因?yàn)椋珹ndroid中不同的URl可以由我們自定義的Activity組件來處理,而不一定是Webkit,實(shí)現(xiàn)方法如下:
編寫入口Acitivity實(shí)現(xiàn)如下代碼:
package com.qqtest;
import com.qq.weibo.OAuth;
import com.sdhjob.util.ConfigUtil;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.DownloadListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.DownloadListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String key = ConfigUtil.getValue("qq.weibo.appKey");
String secret = ConfigUtil.getValue("qq.weibo.appSecret");
OAuth oAuth = new OAuth(key,secret);
WebView w=(WebView)this.findViewById(R.id.ok);
// 運(yùn)行完后注釋掉這段 放開 32行到37行的代碼
String oauthToken = oAuth.getOauthToken();
Log.d("qq","oauthToken="+oauthToken);
if( this.getIntent()!=null&&this.getIntent().getData()!=null)
{
Uri uri = this.getIntent().getData();
//驗(yàn)證碼
String oauth_verifier = uri.getQueryParameter("oauth_verifier");
Toast.makeText(this,"驗(yàn)證碼"+oauth_verifier, 1000).show();
}else
if(oauthToken != null){
final String url=" https://open.t.qq.com/cgi-bin/authorize?"+oauthToken ;
Intent it=new Intent(Intent.ACTION_VIEW,Uri.parse(url));
this.startActivity(it);
}
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String key = ConfigUtil.getValue("qq.weibo.appKey");
String secret = ConfigUtil.getValue("qq.weibo.appSecret");
OAuth oAuth = new OAuth(key,secret);
WebView w=(WebView)this.findViewById(R.id.ok);
// 運(yùn)行完后注釋掉這段 放開 32行到37行的代碼
String oauthToken = oAuth.getOauthToken();
Log.d("qq","oauthToken="+oauthToken);
if( this.getIntent()!=null&&this.getIntent().getData()!=null)
{
Uri uri = this.getIntent().getData();
//驗(yàn)證碼
String oauth_verifier = uri.getQueryParameter("oauth_verifier");
Toast.makeText(this,"驗(yàn)證碼"+oauth_verifier, 1000).show();
}else
if(oauthToken != null){
final String url=" https://open.t.qq.com/cgi-bin/authorize?"+oauthToken ;
Intent it=new Intent(Intent.ACTION_VIEW,Uri.parse(url));
this.startActivity(it);
}
}
}
紅色部分是怎么實(shí)現(xiàn)的呢,看androidmanifest .xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=" http://schemas.android.com/apk/res/android "
package="com.qqtest"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainTest"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="qqweibook" />
<data android:host="qqweibo.sdhjob" />
</intent-filter>
</activity>
<manifest xmlns:android=" http://schemas.android.com/apk/res/android "
package="com.qqtest"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainTest"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="qqweibook" />
<data android:host="qqweibo.sdhjob" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
///////////////////////////////////////////
<data android:host是在請(qǐng)求騰訊的api時(shí)候加入的參數(shù)
oauth_callback = "qqweibook://qqweibo.sdhjob"; // —— 用戶授權(quán)后的返回地址
///////////////////////////////////////////////////////////////////
至于怎么使用騰訊API不說了,你懂的............................................................................
2011年沈大海講師Android的騰訊微博客戶端源代碼,實(shí)現(xiàn)oauth_verifier自動(dòng)提取!
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
