如果大家開發過Android版的新浪微博客戶端可以通過如下接口直接在客戶端返回oauth_verifier
以下是我加的方法,注意增加了userId和passwd參數,大小寫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
}
但是在騰訊中沒有該方法,只能彈出頁面,之后就跳轉到了另一個url,而oauth_verifier在url中,不能通過代碼直接獲取,其實在Android中式可以解決的,如下圖:
因為,Android中不同的URl可以由我們自定義的Activity組件來處理,而不一定是Webkit,實現方法如下:
編寫入口Acitivity實現如下代碼:
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);
// 運行完后注釋掉這段 放開 32行到37行的代碼
String oauthToken = oAuth.getOauthToken();
Log.d("qq","oauthToken="+oauthToken);
if( this.getIntent()!=null&&this.getIntent().getData()!=null)
{
Uri uri = this.getIntent().getData();
//驗證碼
String oauth_verifier = uri.getQueryParameter("oauth_verifier");
Toast.makeText(this,"驗證碼"+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);
// 運行完后注釋掉這段 放開 32行到37行的代碼
String oauthToken = oAuth.getOauthToken();
Log.d("qq","oauthToken="+oauthToken);
if( this.getIntent()!=null&&this.getIntent().getData()!=null)
{
Uri uri = this.getIntent().getData();
//驗證碼
String oauth_verifier = uri.getQueryParameter("oauth_verifier");
Toast.makeText(this,"驗證碼"+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);
}
}
}
紅色部分是怎么實現的呢,看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是在請求騰訊的api時候加入的參數
oauth_callback = "qqweibook://qqweibo.sdhjob"; // —— 用戶授權后的返回地址
///////////////////////////////////////////////////////////////////
至于怎么使用騰訊API不說了,你懂的............................................................................
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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