我正在使用适用于 Android 的 Google API。由于适用于 android 的 Google API/G Suite Quickstart 引用了他们的 java 示例,因此我正在尝试实现它:
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
ReadMail.HTTP_TRANSPORT, ReadMail.JSON_FACTORY, clientSecrets, ReadMail.SCOPES)
.setDataStoreFactory(ReadMail.DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
AuthorizationCodeInstalledApp authCodeInstalledApp = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver());
Credential credential = authCodeInstalledApp.authorize("user");
我现在遇到的问题是 Android 只支持 JDK 类的一个子集。因此不支持 java.awt.Desktop。
但我确实需要这个类,因为 AuthorizationCodeInstalledApp
的 authorize()
迟早会调用它的内部函数 browse()
。此功能需要 Desktop 类。
有没有办法为 android 获取该类?或者是否有其他解决方法来通过 Google 进行身份验证?
最佳答案
我现在自己解决了。我没有尝试从 java.awt.Desktop 获取 Desktop 类,而是重写了 on Authorization 方法:
AuthorizationCodeInstalledApp ab = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()){
protected void onAuthorization(AuthorizationCodeRequestUrl authorizationUrl) throws IOException {
String url = (authorizationUrl.build());
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
main_Activity.startActivity(browserIntent);
}
};
我这样做的原因是因为 authorize() 将调用 onAuthorization(),而 onAuthorization() 将调用 browse(),后者检查是否支持桌面。 因此,通过重写 onAuthorization() 方法,我将不再需要该类。 我重写的类将在您的 Android 设备中使用该授权 URL 启动一个新的浏览器窗口。
我希望,我能够帮助遇到这个问题的任何人。
关于java.awt.桌面类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52850911/