简体   繁体   中英

How to get Google OAuth2 Token with Webview

I am trying to implement OAuth2 within my android app using webviews.

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?

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)

There's seems to be no method to get the page html, so I can't parse it and grab that way.

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.

Is there something I'm missing in trying to obtain the token?

I assume you are using token as response_type.

One way to do this is to override the shouldOverrideUrlLoading method of WebViewClient:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM