[英]How to get Google OAuth2 Token with Webview
I am trying to implement OAuth2 within my android app using webviews. 我正在尝试使用webviews在我的android应用程序中实现OAuth2。
One issue I am having is after the user allows my app to access their account, I am redirected to the page that contains the token, but I can't grab the token and set it to a variable. 我遇到的一个问题是,在用户允许我的应用访问他们的帐户之后,我被重定向到了包含令牌的页面,但是我无法获取令牌并将其设置为变量。
How can I get this token automatically within my WebViewClient onPageFinished method? 如何在WebViewClient onPageFinished方法中自动获取此令牌?
I was originally going to go by the page title, which is easy to get, but I noticed that the page title seems to have a truncated token (if the token is asdfgh.12345 the title only contains asdfgh) 我原本打算按页面标题进行操作,这很容易获得,但是我注意到页面标题似乎具有截断的标记(如果标记为asdfgh.12345,则标题仅包含asdfgh)
There's seems to be no method to get the page html, so I can't parse it and grab that way. 似乎没有获取页面html的方法,所以我无法解析它并采用这种方式。
I read somewhere that an oauth_token cookie should be set, but unless I'm grabbing the cookies incorrectly, google does not seem to set an oauth_token cookie. 我在某处读到应该设置oauth_token cookie,但除非我不正确地获取了cookie,否则Google似乎不会设置oauth_token cookie。
Is there something I'm missing in trying to obtain the token? 我在尝试获取令牌时缺少什么?
I assume you are using token as response_type. 我假设您使用令牌作为response_type。
One way to do this is to override the shouldOverrideUrlLoading method of WebViewClient: 一种方法是重写WebViewClient的shouldOverrideUrlLoading方法:
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith(REDIRECT_URI)) {
// grab the token from the url
...
return true;
}
return false;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.