android 上的 FileDataStoreFactory 和 posix?
FileDataStoreFactory and posix on android?
我正在考虑在 android 上制作一个简单的日历应用程序,并且正在考虑使用 google 日历 API 和目标 SDK 24 及更高版本,但我看到了一个潜力问题,google 没有 this on android, but they do have a java implementation, which leads to another problem, a hidden POSIX implementation which I'm not sure how to get around, it's inside their new FileDataStoreFactory(...)
method 的等效实现。我有什么选择,有什么办法解决这个问题吗?如果我在 SDK 26 上 运行 它工作正常,授权流程 .setDataStoreFactory 方法,但低于该方法我将收到以下错误:
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/nio/file/attribute/PosixFilePermission;
at com.google.api.client.util.store.FileDataStoreFactory.setPermissionsToOwnerOnly(FileDataStoreFactory.java:141)
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
* * * this is the part where i will struggle if i have to use an SDK lower than 26 * * *
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
有关更多参考,请查看他们的 github 以获得 FileDataStoreFactory class ,第 77 行是问题发生的地方 setPermissionsToOwnerOnly(dataDirectory);
在做了更多的挖掘之后,我相信我已经找到了为什么路径在 FDSF 中不起作用的线索,因为最小值 SDK requirement 'Paths was Added in API level 26'
我已经解决了这个问题,所以我避免使用使用 API 26 条路径的 DataStoreFactory,而是使用了一个很好的旧 mkdir 并相应地更改了它的权限。之后,我使用凭据存储进行设置,现在我的应用程序可以使用 google 日历 api 使用 SDK 24、SDK 25 和 SDK 26+。我还想提一下,我们花了一些繁重的工作来挖掘与此相关的过时问题,以及手动挖掘 DataStoreFactory 的实际实现以更好地理解。
private val TOKENS_DIRECTORY_PATH: String = "tokens"
private val CREDENTIALS_FILE_PATH: String = "credentials.json"
val inputStream: InputStream = activity!!.resources!!.assets!!.open(CREDENTIALS_FILE_PATH)
val clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, InputStreamReader(inputStream))
val mDirectory: File? = File(activity.filesDir.absolutePath + File.separator + TOKENS_DIRECTORY_PATH)
if(!mDirectory!!.exists()){
mDirectory.mkdir()
mDirectory.setReadable(true,true)
mDirectory.setWritable(true,true)
mDirectory.setExecutable(true,false)
}
val mFile = File(mDirectory, CREDENTIALS_FILE_PATH)
mFile.setReadable(true, true)
mDirectory.setWritable(true,true)
mDirectory.setExecutable(true,false)
val mFileContents: String =
activity.resources.assets
.open(CREDENTIALS_FILE_PATH).bufferedReader().use { it.readText() }
mFile.outputStream().bufferedWriter().use { out ->
out.write(mFileContents)
}
val mCredentialStore: CredentialStore? = FileCredentialStore(mFile, JSON_FACTORY)
//val fileFactory: FileDataStoreFactory? = FileDataStoreFactory(file)
this.mFlow = GoogleAuthorizationCodeFlow
.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setCredentialStore(mCredentialStore)
.setAccessType(ACCESS_TYPE)
.build()
现在不需要解决方法。 Google 添加了对低于 Android 8 (API 28) 的较低 Android 版本 (API 19+) 的支持,以便于 2020 年 7 月 google-http-java-client in version 1.36.0 with this 提交。只需导入这个,
import com.google.api.client.extensions.android.util.store.FileDataStoreFactory
在您使用 FileDataStoreFactory 的文件中,并在应用的 build.gradle 文件中添加此依赖项,
// Replace 1.38.1 with latest version number from https://github.com/googleapis/google-http-java-client/releases
implementation 'com.google.http-client:google-http-client-android:1.38.1'
我正在考虑在 android 上制作一个简单的日历应用程序,并且正在考虑使用 google 日历 API 和目标 SDK 24 及更高版本,但我看到了一个潜力问题,google 没有 this on android, but they do have a java implementation, which leads to another problem, a hidden POSIX implementation which I'm not sure how to get around, it's inside their new FileDataStoreFactory(...)
method 的等效实现。我有什么选择,有什么办法解决这个问题吗?如果我在 SDK 26 上 运行 它工作正常,授权流程 .setDataStoreFactory 方法,但低于该方法我将收到以下错误:
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/nio/file/attribute/PosixFilePermission;
at com.google.api.client.util.store.FileDataStoreFactory.setPermissionsToOwnerOnly(FileDataStoreFactory.java:141)
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
* * * this is the part where i will struggle if i have to use an SDK lower than 26 * * *
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
有关更多参考,请查看他们的 github 以获得 FileDataStoreFactory class ,第 77 行是问题发生的地方 setPermissionsToOwnerOnly(dataDirectory);
在做了更多的挖掘之后,我相信我已经找到了为什么路径在 FDSF 中不起作用的线索,因为最小值 SDK requirement 'Paths was Added in API level 26'
我已经解决了这个问题,所以我避免使用使用 API 26 条路径的 DataStoreFactory,而是使用了一个很好的旧 mkdir 并相应地更改了它的权限。之后,我使用凭据存储进行设置,现在我的应用程序可以使用 google 日历 api 使用 SDK 24、SDK 25 和 SDK 26+。我还想提一下,我们花了一些繁重的工作来挖掘与此相关的过时问题,以及手动挖掘 DataStoreFactory 的实际实现以更好地理解。
private val TOKENS_DIRECTORY_PATH: String = "tokens"
private val CREDENTIALS_FILE_PATH: String = "credentials.json"
val inputStream: InputStream = activity!!.resources!!.assets!!.open(CREDENTIALS_FILE_PATH)
val clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, InputStreamReader(inputStream))
val mDirectory: File? = File(activity.filesDir.absolutePath + File.separator + TOKENS_DIRECTORY_PATH)
if(!mDirectory!!.exists()){
mDirectory.mkdir()
mDirectory.setReadable(true,true)
mDirectory.setWritable(true,true)
mDirectory.setExecutable(true,false)
}
val mFile = File(mDirectory, CREDENTIALS_FILE_PATH)
mFile.setReadable(true, true)
mDirectory.setWritable(true,true)
mDirectory.setExecutable(true,false)
val mFileContents: String =
activity.resources.assets
.open(CREDENTIALS_FILE_PATH).bufferedReader().use { it.readText() }
mFile.outputStream().bufferedWriter().use { out ->
out.write(mFileContents)
}
val mCredentialStore: CredentialStore? = FileCredentialStore(mFile, JSON_FACTORY)
//val fileFactory: FileDataStoreFactory? = FileDataStoreFactory(file)
this.mFlow = GoogleAuthorizationCodeFlow
.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setCredentialStore(mCredentialStore)
.setAccessType(ACCESS_TYPE)
.build()
现在不需要解决方法。 Google 添加了对低于 Android 8 (API 28) 的较低 Android 版本 (API 19+) 的支持,以便于 2020 年 7 月 google-http-java-client in version 1.36.0 with this 提交。只需导入这个,
import com.google.api.client.extensions.android.util.store.FileDataStoreFactory
在您使用 FileDataStoreFactory 的文件中,并在应用的 build.gradle 文件中添加此依赖项,
// Replace 1.38.1 with latest version number from https://github.com/googleapis/google-http-java-client/releases
implementation 'com.google.http-client:google-http-client-android:1.38.1'