更新时间:2023-09-28 13:05:22
我也做了以下来实现这一点:
I have done the following to achieve this:
该移动应用程序从URL中提取令牌和交谈时,服务器使用它进行认证。
The mobile application extracts the token from the URL and uses it for authentication when talking to the server.
在WebLogin活动如下:(注:你必须重写shouldOverrideUrlLoading留在同一个web视图,否则,一个新的浏览器打开时,您会收到一些重定向)
The WebLogin Activity looks like this: (note: you have to override "shouldOverrideUrlLoading" to stay in the same WebView. Otherwise, a new browser is open when you receive some redirect)
公共类WebLogin延伸活动{
public class WebLogin extends Activity {
@覆盖 保护无效的onCreate(包savedInstanceState){ super.onCreate(savedInstanceState);
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
if(StringUtils.contains(url, "?auth_token=")){
// extract and save token here
setResult(RESULT_OK);
finish();
}
}
}); webview.loadUrl(https://www.yourdomain.com/authapp); webview.getSettings()setJavaScriptEnabled(真)。 的setContentView(web视图);
}); webview.loadUrl("https://www.yourdomain.com/authapp"); webview.getSettings().setJavaScriptEnabled(true); setContentView(webview);
} }
请注意,我使用https,使这个拯救。如果使用普通的http协议,你可以阅读和窃取用户的令牌。
Note, I use https to make this save. If you use plain http, one could read and steal the token of a user.
[1]:http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, INT)
[1]: http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)