android客户端连接人人网之一----获取授权

连接人人网,有两种方法,参考人人网的官网:
http://wiki.dev.renren.com/wiki/%E7%A7%BB%E5%8A%A8%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%8E%A5%E5%85%A5
第一种是直接使用人人网开放平台提供的各种接口,如用作验证和授权的OAuth 2.0,提供数据的底层Rest API,以及嵌入各种Widget。
第二种是使用人人网开放平台官方封装的开源Android SDK。
我们使用第一种:
首先去申请一个应用: http://dev.renren.com/app
完成后你将得到:API Key和Secret Key即“client_id”和“client_secret”,便可以使用OAuth2.0进行验证授权。开发者中心页面中API Key就是人人OAuth2.0中的“client_id”,Secret Key就是“client_secret”。
人人网为没有Web服务器的客户端应用提供了一个通用的URL:http://graph.renren.com/oauth/login_success.html。

流程如下:

在应用中嵌入一个浏览器控件,并使用客户端流程定向控件到人人OAuth 2.0 Authorize Endpoint(https://graph.renren.com/oauth/authorize):

https://graph.renren.com/oauth/authorize?client_id=YOUR_API_KEY&redirect_uri=http://graph.renren.com/oauth/login_success.html

经过用户验证、应用授权,人人OAuth2.0将把浏览器控件定向导'redirect_uri'(http://graph.renren.com/oauth/login_success.html),并在URI Fragment中追加Access Token:http://graph.renren.com/oauth/login_success.html#access_token=...当应用发现浏览器的控件的URL跳转到这个URL上时,从URL中解析出Access Token。



在Android中加载html用WebView控件,由于Android主要是触屏,因此需要加display=touch参数,完整的URL为

https://graph.renren.com/oauth/authorize?client_id=05d3794614f244c39e300c65f5f68a9e&response_type=token&display=touch&redirect_uri=http://graph.renren.com/oauth/login_success.html

具体登录的代码如下:
public class RenrenLoginActivity extends Activity {  
    public final static String TAG = "RenrenLoginActivity";  
    private WebView renrenLoginWebView; // WebView 控件,用于显示从人人网请求得到html授权页面  
      
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.renren_login_web_view);  
        renrenLoginWebView = (WebView) findViewById(R.id.renren_login_web_view); // 得到 WebView 控件  
          
        //对WebView进行设置(对JS的支持,对缩放的支持,对缓存模式的支持)  
        WebSettings webSettings = renrenLoginWebView.getSettings();  
        webSettings.setJavaScriptEnabled(true);   
        webSettings.setBuiltInZoomControls(true);  
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);  
          
        // 根据client_id取得到人人服务器人人对你的应用授权,如果成功则返回人人网登陆页面的html文件,并在WebView控件上显示  
        // 此时用户需要输入自己人人账号的用户名、密码并点击登陆  
        renrenLoginWebView.loadUrl("https://graph.renren.com/oauth/authorize?"+  
                "client_id=换成你自己的api key&response_type=token"+  
                "&display=touch&redirect_uri=http://graph.renren.com/oauth/login_success.html");  
          
        renrenLoginWebView.setWebViewClient(new WebViewClient() {  
  
            //击网页里面的链接还是在当前的webview里跳转,不跳到浏览器那边  
            @Override  
            public boolean shouldOverrideUrlLoading(WebView view, String url) {  
                view.loadUrl(url);  
                return true;  
            }  
  
            @Override  
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {  
                handler.proceed();//让webview处理https请求  
            }  
              
            @Override  
            public void onPageFinished(WebView view, String url) {  
                String url0 = renrenLoginWebView.getUrl();  
                String access_token = "";  
                String expires_in = "";  
                Log.i(TAG, "URL = " + url0);  
                if(url0 != null) {  
                    if(url0.contains("access_token=")) { // 从URL中解析得到 access_token  
                        access_token = url0.substring(url0.indexOf("access_token=") + 13, url0.length() - 19);  
                        try {  
                            access_token = URLDecoder.decode(access_token, "utf-8"); // 制定为utf-8编码  
                        } catch (UnsupportedEncodingException e) {  
                            e.printStackTrace();  
                        }  
                        Log.i(TAG, "access_token = " + access_token);  
                    }  
                    if(url0.contains("expires_in=")) { // 从URL中解析得到 expires_in  
                        expires_in = url0.substring(url0.indexOf("expires_in=") + 11, url0.length());  
                        Log.i(TAG, "expires_in = " + expires_in);  
                    }  
                    RenrenUtil.access_token = access_token; // 将解析得到的 access_token 保存起来  
                    RenrenUtil.expires_in = expires_in; // 将解析得到的 expires_in 保存起来  
                      
                    //输入用户名、密码登陆成功,进行页面跳转  
                    if(RenrenUtil.access_token.length() != 0) {  
                        Intent intent = new Intent(RenrenLoginActivity.this, RenrenFriendsActivity.class);  
                        startActivity(intent);  
                    }  
                }  
                super.onPageFinished(view, url);  
            }             
        });  
    }  
}  

经测试,成功。

猜你喜欢

转载自ericchan2012.iteye.com/blog/1677797