텐 센트 웨 이 보 OAuthV 2 인증 제3자 로그 인 실현

11617 단어 OAuth
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. 자바
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);

    }



}

좋은 웹페이지 즐겨찾기