现在的位置: 首页 > 综合 > 正文

2011年沈大海讲师Android的腾讯微博客户端源代码,实现oauth_verifier自动提取!

2013年10月16日 ⁄ 综合 ⁄ 共 3126字 ⁄ 字号 评论关闭

如果大家开发过Android版的新浪微博客户端可以通过如下接口直接在客户端返回oauth_verifier

 

 以下是我加的方法,注意增加了userId和passwd参数,大小写userId的I是大写,该问题困扰了我1晚上。

lpublic 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;
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);
  }
   }
}
红色部分是怎么实现的呢,看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>
    </application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
///////////////////////////////////////////
<data android:host是在请求腾讯的api时候加入的参数
oauth_callback = "qqweibook://qqweibo.sdhjob"; // —— 用户授权后的返回地址
///////////////////////////////////////////////////////////////////
至于怎么使用腾讯API不说了,你懂的............................................................................

抱歉!评论已关闭.