我使用 android studio 創建了一個 java 應用程式。在專案中,我創建了“assets”檔案夾,并在其中放置了獲取 GmailAPI 憑據所需的 json 檔案。
資產檔案夾
我希望我可以訪問 json 檔案。我已經嘗試了很多方法,但每次它都告訴我檔案丟失了。這是代碼:
public class GmailApi {
public Gmail initService(String userId) throws GeneralSecurityException,
IOException {
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
final GoogleCredentials creds = ServiceAccountCredentials.fromStream(new FileInputStream("gmailapi-android.json"))
.createScoped(GmailScopes.GMAIL_SEND);
final GoogleCredentials delegatedCreds = creds.createDelegated("[email protected]");
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(delegatedCreds);
HttpTransport transport = new NetHttpTransport.Builder().build();
// Construct the gmail object.
Gmail service = new Gmail.Builder(transport, jsonFactory, requestInitializer)
.setApplicationName("gmailapi")
.build();
return service;
}
}
有人可以幫我嗎?我應該在路徑中放入什么才能訪問 json 檔案?
final GoogleCredentials creds = ServiceAccountCredentials.fromStream(new FileInputStream("gmailapi-android.json"))
uj5u.com熱心網友回復:
您是否嘗試以這種方式打開檔案?
AssetManager assetManager = getAssets();
InputStream ims = assetManager.open("gmailapi-android.json");
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/477363.html
