我从帐户管理器获取访问令牌以访问所有谷歌Java客户端API,但我需要打开具有谷歌的WebView,必须通过从帐户管理器获取的访问令牌登录.任何人都知道这一点.
我尝试使用cookies(通过这个),但我无法实现它.如果您知道这种方式,请分享代码段.
我通过使用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();