我正在嘗試使用 oauth2 訪問我的電子郵件并與帳戶和桌面 api 互動-接收和發送電子郵件-我在 java 中使用了帶有 gradle https://developers.google.com/gmail/api/quickstart/的示例java ,我得到這個錯誤:
執行緒“主”java.io.FileNotFoundException 中的例外:找不到資源:C:/Users/xxx/Documents/tableaux dir/prjet 電子郵件 gmail/src/main/ressources/credential.json 在 GmailQuickstart.getCredentials(GmailQuickstart.java: 50) 在 GmailQuickstart.main(GmailQuickstart.java:69)
請問我能做什么?
編碼
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.GmailScopes;
import com.google.api.services.gmail.model.Label;
import com.google.api.services.gmail.model.ListLabelsResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;
/* class to demonstrate use of Gmail list labels API */
public class GmailQuickstart {
/** Application name. */
private static final String APPLICATION_NAME = "Gmail API Java Quickstart";
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
/** Directory to store authorization tokens for this application. */
private static final String TOKENS_DIRECTORY_PATH = "tokens";
/**
* Global instance of the scopes required by this quickstart.
* If modifying these scopes, delete your previously saved tokens/ folder.
*/
private static final List<String> SCOPES = Collections.singletonList(GmailScopes.GMAIL_LABELS);
private static final String CREDENTIALS_FILE_PATH = "C:/Users/xxx/Documents/tableaux dir/prjet email gmail/src/main/ressources/credential.json";
/**
* Creates an authorized Credential object.
* @param HTTP_TRANSPORT The network HTTP Transport.
* @return An authorized Credential object.
* @throws IOException If the credentials.json file cannot be found.
*/
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
// Load client secrets.
InputStream in = GmailQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
//returns an authorized Credential object.
return credential;
}
public static void main(String... args) throws IOException, GeneralSecurityException {
// Build a new authorized API client service.
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
// Print the labels in the user's account.
String user = "me";
ListLabelsResponse listResponse = service.users().labels().list(user).execute();
List<Label> labels = listResponse.getLabels();
if (labels.isEmpty()) {
System.out.println("No labels found.");
} else {
System.out.println("Labels:");
for (Label label : labels) {
System.out.printf("- %s\n", label.getName());
}
}
}
}
uj5u.com熱心網友回復:
轉到谷歌云控制臺并創建一個 cedetials.json 檔案,確保為已安裝/桌面或本機應用程式創建它。
- 創建已安裝的憑據
它會提示您下載一個 json 檔案。將此檔案放在由 CREDENTIALS_FILE_PATH 表示的目錄中。
在您的情況下,檔案應該在此處使用此名稱,這實際上不是路徑,而是檔案的全名。因此,請確保您沒有為它使用不同的名稱或最后沒有額外的 .json。
"C:/Users/xxx/Documents/tableaux dir/prjet email gmail/src/main/ressources/credential.json"
uj5u.com熱心網友回復:
您缺少憑據檔案。在您提供的鏈接中,它說這是一個先決條件。創建它并填充值或從該鏈接下載并將其粘貼到該位置C:/Users/xxx/Documents/tableaux dir/prjet email gmail/src/main/ressources,然后將值更改為您的設定。
此頁面可以幫助創建您的憑據 https://developers.google.com/workspace/guides/create-credentials
uj5u.com熱心網友回復:
正如這里所說,getResourceAsStream() 回傳 null。屬性檔案未加載 “GmailQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH)”函式使用類路徑,因此需要相對路徑。所以 String CREDENTIALS_FILE_PATH = "credentials.json" 就足夠了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/480789.html
