使用客户经理令牌在webview中登录Google帐户

 穿靴子的猫 发布于 2023-01-07 13:14

我从帐户管理器获取访问令牌以访问所有谷歌Java客户端API,但我需要打开具有谷歌的WebView,必须通过从帐户管理器获取的访问令牌登录.任何人都知道这一点.

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

1 个回答
  • 我通过使用ubertoken的cookie找到了这样做的方法.这个函数应该在单独的线程中调用

     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();
    

    2023-01-07 13:16 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有