zoukankan      html  css  js  c++  java
  • android 腾讯微博登录小demo

    目前越来越多的app,都需要集成三方登录(腾讯微博、新浪微博、人人等等)。这里分享一个简单的腾讯微博demo。废话不多说,三步走。


    第一步注册一个账户,并创建一个应用。获取app ID与 app Key。

    具体地址:http://open.qq.com

    第二步下载sdk

    具体地址:http://wiki.opensns.qq.com/wiki/%E3%80%90QQ%E7%99%BB%E5%BD%95%E3%80%91SDK%E4%B8%8B%E8%BD%BD 

    第三步新建工程,修改清单文件,导入相关的sdk文件及调用相应的api搞定。

    3.1 修改清单文件,主要是加入一个webview的activity

           <activity
                android:name="com.tencent.weibo.webview.OAuthV2AuthorizeWebView"
                android:label="@string/app_name" >
            </activity>


    3.2  将Android_SDK_v1.2.jar与httpmime-4.1.3.jar导入libs中就好。

    3.3 在需要三方登录的地方,调用相应的api即可。


    下面是小demo工程的清单文件及activity中api代码简单示例。

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.test.chesterweibodemo"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk android:minSdkVersion="10" />
    
        <!-- 允许网络访问 -->
        <uses-permission android:name="android.permission.INTERNET" />
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <!-- demo activity,调用api -->
            <activity
                android:name=".ChesterWeiboDemoActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <!-- OAuth Version 2. 使用  WebView 辅助进行ImplicitGrant方式授权必须 -->
            <activity
                android:name="com.tencent.weibo.webview.OAuthV2AuthorizeWebView"
                android:label="@string/app_name" >
            </activity>
        </application>
    
    </manifest>


    ChesterWeiboDemoActivity  代码如下:

    package com.test.chesterweibodemo;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.util.Log;
    import android.widget.TextView;
    
    import com.tencent.weibo.api.UserAPI;
    import com.tencent.weibo.constants.OAuthConstants;
    import com.tencent.weibo.oauthv2.OAuthV2;
    import com.tencent.weibo.webview.OAuthV2AuthorizeWebView;
    
    /**
     * @author chensf5 2013-1-22
     */
    public class ChesterWeiboDemoActivity extends Activity {
    	private static final String TAG = "ChesterWeiboDemoActivity";
    	private OAuthV2 oAuth;
    
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		//验证回调url地址,随便填写个 http://www.tencent.com/zh-cn/index.shtml
    		oAuth = new OAuthV2("http://apk.91.com/Soft/Android/com.palmit.player-1-1.0.html"); 
    		oAuth.setClientId("801297210"); // 801115505
    		oAuth.setClientSecret("d163aeecdc7a9e5a601b03d66d4265be"); // be1dd1410434a9f7d5a2586bab7a6829
    
    		Intent intent = new Intent(ChesterWeiboDemoActivity.this,
    				OAuthV2AuthorizeWebView.class);
    		intent.putExtra("oauth", oAuth);
    		startActivityForResult(intent, 1); //开启webview加载个html页面
    	}
    
    	private Handler handler = new Handler() {
    		public void handleMessage(Message msg) {
    			if (null != msg.obj && msg.obj instanceof String) {
    				String response = (String) msg.obj;
    				((TextView) findViewById(R.id.tv_content)).setText(response
    						+ "\n");
    				Log.i(TAG, response);
    				Log.i(TAG + "----------",
    						"redirectUri:" + oAuth.getRedirectUri() + ",clientId:"
    								+ oAuth.getClientId() + ",clientSecret:"
    								+ oAuth.getClientSecret() + ",responseType:"
    								+ oAuth.getResponeType() + ",type:"
    								+ oAuth.getType() + ",authorizeCode:"
    								+ oAuth.getAuthorizeCode() + ",accessToken:"
    								+ oAuth.getAccessToken() + ",expiresIn:"
    								+ oAuth.getExpiresIn() + ",grantType:"
    								+ oAuth.getGrantType() + ",refreshToken:"
    								+ oAuth.getRefreshToken() + ",openid:"
    								+ oAuth.getOpenid() + "," + oAuth.getOpenkey());
    			}
    		};
    	};
    
    	protected void onActivityResult(int requestCode, int resultCode,
    			final Intent data) {
    		if (requestCode == 1) {
    			if (resultCode == OAuthV2AuthorizeWebView.RESULT_CODE) {
    
    				new Thread() {
    					public void run() {
    						oAuth = (OAuthV2) data.getExtras().getSerializable("oauth");
    
    						// 调用API获取用户信息
    						UserAPI userAPI = new UserAPI(
    								OAuthConstants.OAUTH_VERSION_2_A);
    						try {
    							String response = userAPI.info(oAuth, "json");// 获取用户信息
    
    							Message msg = handler.obtainMessage();
    
    							msg.obj = response;
    							handler.sendMessage(msg);
    						} catch (Exception e) {
    							e.printStackTrace();
    						}
    						userAPI.shutdownConnection();
    					};
    				}.start();
    
    			} else {
    				Log.i(TAG, "返回过来的不对");
    			}
    
    		} else {
    			Log.i(TAG, "没有授权可拿");
    		}
    
    	}
    }


      Demo工程已上传至csdn免积分下载中,尚在审核中。过两天有具体地址再补上。

       审核通过,csdn效率还是蛮高的,下载地址:http://download.csdn.net/detail/chenshufei2/5289129

  • 相关阅读:
    AngularJs 键盘事件和鼠标事件
    Linux的net.ipv4.tcp_timestamps参数
    实战:tcp链接rst场景tcpdump分析
    C++ map使用总结
    C++ 11常见功能介绍:auto,decltype,nullptr,for,lambda
    BLOCK层基本概念:bio,request,request_queue
    C: 字符数组中的空去掉
    代码静态分析工具-splint的学习与使用[转]
    代码分析工具splint安装介绍
    gcc/g++ 如何支持c11/c++11标准编译
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3037310.html
Copyright © 2011-2022 走看看