开发者社区> 西北野狼> 正文

腾讯微博OAuthV2认证实现第三方登录

简介: 1)添加相关jar包: httpmime.jar 以下信息是必须的 //!!!请根据您的实际情况修改!!! 认证成功后浏览器会被重定向到这个url中 必须与注册时填写的一致 private String redirectUri="http://www.
+关注继续查看

1)添加相关jar包:

httpmime.jar

以下信息是必须的

//!!!请根据您的实际情况修改!!!      认证成功后浏览器会被重定向到这个url中  必须与注册时填写的一致
    private String redirectUri="http://www.tencent.com/zh-cn/index.shtml";                   
    //!!!请根据您的实际情况修改!!!      换为您为自己的应用申请到的APP KEY
    private String clientId = "APP KEY"; 
    //!!!请根据您的实际情况修改!!!      换为您为自己的应用申请到的APP SECRET
    private String clientSecret="APP SECRET";

    private OAuthV2 oAuth;
在oncreate()里面处理
    oAuth=new OAuthV2(redirectUri);
        oAuth.setClientId(clientId);
        oAuth.setClientSecret(clientSecret);

        //关闭OAuthV2Client中的默认开启的QHttpClient。
        OAuthV2Client.getQHttpClient().shutdownConnection();

 

2)在你的Activity中添加如下代码:

 intent = new Intent(你的Activity.this, OAuthV2AuthorizeWebView.class);//创建Intent,使用WebView让用户授权
                        intent.putExtra("oauth", oAuth);
                        startActivityForResult(intent,2);  

 

  /*
     * 通过读取OAuthV2AuthorizeWebView返回的Intent,获取用户授权信息
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent data)   {
        if (requestCode==2) {
            if (resultCode==OAuthV2AuthorizeWebView.RESULT_CODE)    {
                oAuth=(OAuthV2) data.getExtras().getSerializable("oauth");
                if(oAuth.getStatus()==0)
                    Toast.makeText(getApplicationContext(), "登陆成功", Toast.LENGTH_SHORT).show();
            }
        }
    }

3)官方登录界面OAuthV2AuthorizeWebView.java

package com.tencent.weibo.webview;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.tencent.weibo.oauthv2.OAuthV2;
import com.tencent.weibo.oauthv2.OAuthV2Client;

/**
 * 使用Webview显示OAuth Version 2.a ImplicitGrant方式授权的页面<br>
 * (移动终端不建议使用Authorize code grant方式授权)<br>
 * <p>本类使用方法:</p>
 * <li>需要调用本类的地方请添加如下代码
 * <pre>
 * //请将OAuthV2Activity改为所在类的类名
 * Intent intent = new Intent(OAuthV2Activity.this, OAuthV2AuthorizeWebView.class);   
 * intent.putExtra("oauth", oAuth);  //oAuth为OAuthV2类的实例,存放授权相关信息
 * startActivityForResult(intent, myRrequestCode);  //请设置合适的requsetCode
 * </pre>
 * <li>重写接收回调信息的方法
 * <pre>
 * if (requestCode==myRrequestCode) {  //对应之前设置的的myRequsetCode
 *     if (resultCode==OAuthV2AuthorizeWebView.RESULT_CODE) {
 *         //取得返回的OAuthV2类实例oAuth
 *         oAuth=(OAuthV2) data.getExtras().getSerializable("oauth");
 *     }
 * }
 * <pre>
 * @see android.app.Activity#onActivityResult(int requestCode, int resultCode,  Intent data)
 */
public class OAuthV2AuthorizeWebView extends Activity {
    public final static int RESULT_CODE = 2;
    private static final String TAG = "OAuthV2AuthorizeWebView";
    private OAuthV2 oAuth;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout linearLayout = new LinearLayout(this);
        WebView webView = new WebView(this);
        linearLayout.addView(webView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        setContentView(linearLayout);
        Intent intent = this.getIntent();
        oAuth = (OAuthV2) intent.getExtras().getSerializable("oauth");
        String urlStr = OAuthV2Client.generateImplicitGrantUrl(oAuth);

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true);
        webView.requestFocus();
        webView.loadUrl(urlStr);
        System.out.println(urlStr.toString());
        Log.i(TAG, "WebView Starting....");
        WebViewClient client = new WebViewClient() {
            /**
             * 回调方法,当页面开始加载时执行
             */
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                Log.i(TAG, "WebView onPageStarted...");
                Log.i(TAG, "URL = " + url);
                if (url.indexOf("access_token=") != -1) {
                    int start=url.indexOf("access_token=");
                    String responseData=url.substring(start);
                    OAuthV2Client.parseAccessTokenAndOpenId(responseData, oAuth);
                    Intent intent = new Intent();
                    intent.putExtra("oauth", oAuth);
                    setResult(RESULT_CODE, intent);
                    view.destroyDrawingCache();
                    view.destroy();
                    finish();
                }
                super.onPageStarted(view, url, favicon);
            }

            /*
             * TODO Android2.2及以上版本才能使用该方法 
             * 目前https://open.t.qq.com中存在http资源会引起sslerror,待网站修正后可去掉该方法
             */
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                if ((null != view.getUrl()) && (view.getUrl().startsWith("https://open.t.qq.com"))) {
                    handler.proceed();// 接受证书
                } else {
                    handler.cancel(); // 默认的处理方式,WebView变成空白页
                }
                // handleMessage(Message msg); 其他处理
            }
        };
        webView.setWebViewClient(client);
    }

}

 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
微信第三方登录
微信第三方登录
45 0
微信开放平台开发(2) 网站应用微信登录
关键字:微信公众平台 微信开放平台 微信登录 微信扫码登录 使用微信账号登录网站作者:方倍工作室 原文:http://www.cnblogs.com/txw1958/p/weixin-qrlogin.html     在这篇微信公众平台开发教程中,我们将介绍如何使用微信开放平台接口实现微信扫码登录的功能。
1787 0
关于微博认证和微信认证
微博认证需要有已经认证的新浪微博或者腾讯微博账号,需要粉丝为500人以上,认证后账号将显示微博认证。认证以后功能上和没有认证并没有什么区别,但搜索的时候可以排名靠前。微信认证起初只有部分合作账号有,比如:深圳东部华侨城。
934 0
微信开放平台 公众号第三方平台开发 教程三 一键登录授权给第三方平台
原文:微信开放平台 公众号第三方平台开发 教程三 一键登录授权给第三方平台 教程导航: 微信开放平台 公众号第三方平台开发 教程一 平台介绍 微信开放平台 公众号第三方平台开发 教程二 创建公众号第三方平台 微信开放平台 公众号第三方平台开发 教程三 一键登录授权给第三方平台  微信开放平台 公众号第三方平台开发 教程四 代公众号调用接口的SDK和demo   公众号第三方平台的开放,是为了让公众号运营者,在面向垂直行业需求时,可以一键登录授权给第三方的公众号运营平台,通过第三方开发者提供的公众号第三方平台来完成相关业务。
1570 0
微信开发笔记——微信网页登录授权,获取用户信息
原文:微信开发笔记——微信网页登录授权,获取用户信息 微信网页授权,获取用户的微信官方API文档地址:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.
1236 0
微信开放平台开发(3) 移动应用微信登录
关键字:微信公众平台 微信开放平台 微信登录 移动应用微信登录 使用微信账号登录APP作者:方倍工作室 原文:http://www.cnblogs.com/txw1958/p/weixin-applogin.html     在这篇微信公众平台开发教程中,我们将介绍如何使用微信开放平台接口实现移动应用微信登录的功能。
2085 0
第三方登陆之微博登陆
前言: 随着互联网的高速发展我们我们注册了各种各种的会员,很多时候我们都不记得是否注册果这个网站的会员,或者忘记了用户名或者密码,我们越来越不想填注册表单了.
1292 0
+关注
西北野狼
擅长Android ,J2EE开发 博客园地址:http://www.cnblogs.com/androidsuperman/p/7834762.html github地址: https://github.com/soyoungboy
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载