gpt4 book ai didi

googles Credential.refreshToken() 中的 android NetworkOnMainThreadException

转载 作者:行者123 更新时间:2023-11-30 02:54:28 25 4
gpt4 key购买 nike

我有一个 Android 应用程序,它应该使用谷歌融合表。我使用的是谷歌服务帐户,必须获取我的 xxxxxxxxxxxprivatekey.p12 的路径。

public class CredentialProvider {

...

private static String PRIVATE_KEY = "xxxxxxxxxxxprivatekey.p12";

...

public static GoogleCredential getCredential() throws IOException, GeneralSecurityException {
return getCredential(Arrays.asList(SCOPES), SERVICE_ACCOUNT_EMAIL, new File(PRIVATE_KEY), HTTP_TRANSPORT, JSON_FACTORY);
}

...

}

代码想要从 PRIVATE_KEY 路径创建一个新文件。我尝试了各种路径,但每次我都遇到 FileNotFoundExceptionopen failed: ENOENT (No such file or directory)

我已经阅读了一些关于 Assets 文件夹的内容,但我不知道如何使用 getCredential 方法让它工作。

  • 我必须将私钥放在我的 android 项目中
  • PRIVATE_KEY 路径看起来如何
  • 如何让“新文件 (PRIVATE_KEY)”起作用

谢谢;)


编辑:

现在我正在覆盖 GoogleCredential.Builder 来创建我自己的 setServiceAccountPrivateKeyFromP12File(InputStream p12File) 就像您的链接一样,它似乎工作正常。但在 getCredential() 中,refreshToken() 被调用并在 NetworkOnMainThreadException 中崩溃。我读过,我应该为此使用 AsyncTask。你能给我一个提示吗,我必须把那个 AsyncTask 放在哪里doInBackground() 里面应该放什么,onPostExecute 里面应该放什么() 或任何方法?

这里是getCredential()的代码。它在 refreshToken() 中崩溃并出现 NetworkOnMainThreadException:

public static GoogleCredential getCredential(List<String> SCOPE, String SERVICE_ACCOUNT_EMAIL, 
InputStream inputStreamFromP12File, HttpTransport HTTP_TRANSPORT, JsonFactory JSON_FACTORY)
throws IOException, GeneralSecurityException {
// Build service account credential.

MyGoogleCredentialBuilder builder = new MyGoogleCredentialBuilder();
builder.setTransport(HTTP_TRANSPORT);
builder.setJsonFactory(JSON_FACTORY);
builder.setServiceAccountId(SERVICE_ACCOUNT_EMAIL);
builder.setServiceAccountScopes(SCOPE);
builder.setServiceAccountPrivateKeyFromP12File(inputStreamFromP12File);

GoogleCredential credential = builder.build();

credential.refreshToken();
return credential;
}

编辑 2:最后,我这样解决了:

private static class RefreshTokenTask extends AsyncTask<GoogleCredential, Void, Void> {
@Override
protected Void doInBackground(GoogleCredential... params) {
try {
params[0].refreshToken();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

在我的 getCredential 方法中:

new RefreshTokenTask().execute(credential);

最佳答案

您不能像访问常规文件那样访问 Assets ,因为这些文件与应用程序 bundle 在一起。这就是为什么 new File(PRIVATE_KEY) 不起作用的原因,并且您无法提供使其起作用的路径。

您可以做的是为该文件获取一个 InputStream :

AssetManager assetManager = context.getAssets();
InputStream input = assetManager.open(PRIVATE_KEY);

如果您需要将其作为文件访问,您可以在应用程序首次启动时将其复制到应用程序的内部存储中。我不确定这是最好的解决方案(出于安全原因,也许您不想将私钥存储在设备的内部存储器中),但它应该可行。然后您可以从 context.getFilesDir () 文件夹访问它。

InputStream fis = assetManager.open(PRIVATE_KEY);
FileOutputStream fos = openFileOutput(PRIVATE_KEY, Context.MODE_PRIVATE);
byte buf[] = new byte[1024];
int len;
while ((len = fis.read(buf)) > 0) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();

关于googles Credential.refreshToken() 中的 android NetworkOnMainThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23523286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com