按照本Java Quickstart指示的步驟。
第1步:打開谷歌API表
一個。使用此嚮導在Google Developers Console中創建或選擇項目並自動打開API。點擊繼續,然後轉到憑據。
b。在向項目頁面添加憑據上,單擊取消按鈕。
c。在頁面頂部,選擇OAuth許可屏幕選項卡。選擇一個電子郵件地址,輸入一個產品名稱(如果尚未設置),然後單擊保存按鈕。 d)。選擇Credentials選項卡,單擊Create credentials按鈕並選擇OAuth客戶端ID。
e。選擇應用程序類型Other,輸入名稱「Google Sheets API Quickstart」,然後單擊Create按鈕。
f。點擊確定關閉結果對話框。 g。
g。單擊客戶端ID右側的file_download(下載JSON)按鈕。 h。
h。將此文件移動到您的工作目錄並將其重命名爲client_secret.json。
當所有設置完成後,這是使用您下載的client_secret.json處理授權的代碼。
public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in =
SheetsQuickstart.class.getResourceAsStream("/client_secret.json");
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(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
Credential credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
System.out.println(
"Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;
}