我从客户经理那里获得了访问 token 以访问所有 google java 客户端 api,但是我需要打开具有 google 的 WebView,它必须通过从客户经理那里获得的访问 token 登录。任何人都知道这一点。
我尝试使用 cookie(通过 this ),但无法实现。如果您知道这种方式,请分享代码 fragment 。
最佳答案
我找到了通过使用 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();
关于android - 使用客户经理 token 在 webview 中登录 Google 帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24451059/