java - Why is GoogleCredentials.fromStream(myJson) coming out null?
问题描述
I have a service account for which I downloaded a Json file to my android studio's 'assets' folder. I then copy/pasted code from google's own documentation and am getting null.
I have tried various ways to read my json file, but all are null.
void AuthExplicit(){
GoogleCredentials credentials = null;
try {
credentials = GoogleCredentials.fromStream(new FileInputStream("auth.json"))
.createScoped(Lists.newArrayList(Collections.singleton("https://www.googleapis.com/auth/cloud-platform")));
}
catch (IOException e) {
e.printStackTrace();
}
new RefreshCred().execute(credentials);
token = credentials.getAccessToken();
tokenString = token.toString();
}
EDIT1 So I have created a raw folder under res and put my auth.json file in there. Then changed my code to the below. credentials is still null.
void AuthExplicit(){
try
{
InputStream stream = getResources().openRawResource(R.raw.auth);
GoogleCredentials credentials = GoogleCredentials.fromStream(stream);
token = credentials.getAccessToken();
tokenString = token.toString();
Log.i("Token",tokenString);
new RefreshCred().execute(credentials);
}
catch (IOException e)
{
e.printStackTrace();
}
}
解决方案
您应该将资产提取到手机上的某个目录,而不是直接从资产文件夹中加载。有关更多详细信息,请查看此答案。
推荐阅读
- sql - 识别 NULL 并更新 oracle 中相同的键列
- javascript - 在 Grafana 中使用 Plotly 的多个持续时间数据箱线图的连续轴
- macos - SwiftUI onCommit 不会将光标移动到下一个文本字段 - MacOS 应用程序
- excel - 如何将我的代码正确地分配给按钮?
- azure - Azure Devops 管道无法访问 Docker Windows 映像?
- android - 如何设置 ChipNavigationBar 的文字颜色
- scala - 通过 Flink 在 Kafka 中动态写入多个 topic 异常处理
- ios - 是否可以将创建领域数据库的脚本从 JSON 添加到我的项目中?
- amazon-web-services - AWS IoT Core Basic Ingest 识别超出的 qoutas
- python - 尽管使用了兼容版本,但 Tensorflow 2.4.0 未检测到 GPU