使用帐户管理员令牌在webview中登录Google帐户 [英] login Google account in webview using account manager token

查看:68
本文介绍了使用帐户管理员令牌在webview中登录Google帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从帐户管理器获得了访问令牌,以访问所有google java客户端api,但是我需要打开具有Google的WebView,必须通过从帐户管理器获得的访问令牌来登录google.任何人对此都有任何想法.

I got the Access token from account Manager to access all google java client api, but I need to open WebView having google which has to be logged in through the access Token got from account manager. Anyone have any idea of this.

我尝试使用Cookie(通过) ,但我无法实现.如果您知道这种方式,请共享代码段.

I tried using cookies (through this), but I couldn't achieve it. If you know that way, please share the code snippet.

推荐答案

我找到了使用ubertoken通过cookie来实现此目的的方法.该函数应在单独的线程中调用

I found the way to do this through cookies by use of ubertoken. this function should call in separate thread

 public void setCookiesToWebView(AccountManager am,Account account,Context context){
    String sid = "";
    String lsid = "";
    Thread uberTokenThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    sid = am.getAuthToken(account, "SID", null, context, null, null)
                            .getResult().getString(AccountManager.KEY_AUTHTOKEN);
                } catch (OperationCanceledException e) {
                    e.printStackTrace();
                } catch (AuthenticatorException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    lsid = am.getAuthToken(account, "LSID", null, context, null, null)
                            .getResult().getString(AccountManager.KEY_AUTHTOKEN);
                } catch (OperationCanceledException e) {
                    e.printStackTrace();
                } catch (AuthenticatorException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


                String TARGET_URL = "https://www.youtube.com/my_live_events";
                Uri ISSUE_AUTH_TOKEN_URL = 
                        Uri.parse("https://www.google.com/accounts/IssueAuthToken?service=gaia&Session=false");
                Uri TOKEN_AUTH_URL = Uri.parse("https://www.google.com/accounts/TokenAuth");

                String url = ISSUE_AUTH_TOKEN_URL.buildUpon().appendQueryParameter("SID", sid)
                        .appendQueryParameter("LSID", lsid)
                        .build().toString();
                HttpPost getUberToken = new HttpPost(url);
                HttpResponse response = null;
                try {
                    response = httpClient.execute(getUberToken);
                } catch (ClientProtocolException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                HttpEntity entity = response.getEntity();
                String uberToken = "";
                try {
                    uberToken = EntityUtils.toString(entity, "UTF-8");
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String getCookiesUrl = TOKEN_AUTH_URL.buildUpon()
                        .appendQueryParameter("source", "android-browser")
                        .appendQueryParameter("auth", uberToken)
                        .appendQueryParameter("continue", TARGET_URL)
                        .build().toString();
                HttpGet getCookies = new HttpGet(getCookiesUrl);

                HttpResponse responseGetCookies = null;
                try {
                    responseGetCookies = httpClient.execute(getCookies);
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                mCookieStore = ((AbstractHttpClient) httpClient).getCookieStore();
                Cookie sessionInfo;
                List<Cookie> cookies = mCookieStore.getCookies();

                if (! cookies.isEmpty()){
                    CookieManager cookieManager = CookieManager.getInstance();
                    cookieManager.removeSessionCookie();
                    CookieSyncManager.createInstance(getApplicationContext());
                    cookieManager.setAcceptCookie(true);
                    for(Cookie cookie : cookies){
                        sessionInfo = cookie;
                        String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue() + "; domain=" + cookie.getDomain();
                        cookieManager.setCookie(sessionInfo.getDomain(), cookieString);
                    }
                    CookieSyncManager.getInstance().sync();
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        });
        uberTokenThread.start();

这篇关于使用帐户管理员令牌在webview中登录Google帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆