[英]Web application deployed not generating authorization tokens
我正在嘗試使用 gmail api 和 oauth2 憑據發送,如下所示
private Credential getCredentials(NetHttpTransport httpTransport) throws IOException {
// Load client secrets.
try {
Resource file = resourceLoader.getResource("classpath:credentials.json");
InputStream inputStream = file.getInputStream();
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
new InputStreamReader(inputStream));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY,
clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline").build();
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
} catch (Exception exception) {
exception.printStackTrace();
LOGGER.info("Exception occured:: {}", exception.getMessage());
throw new RecordNotFoundException(exception.getMessage());
}
}
使用桌面應用程序的 credentials.json 文件。
當我在開發服務器中部署時,我無法生成訪問和刷新令牌保存的文件。
請你幫助我好嗎。
AuthorizationCodeInstalledApp OAuth 2.0 授權代碼流,用於保留最終用戶憑據的已安裝 Java 應用程序。
您不能將其部署到服務器上,它會在服務器上打開授權 window。
對於 web 應用程序,看起來您應該關注此Oauth
public class CalendarServletSample extends AbstractAuthorizationCodeServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
// do stuff
}
@Override
protected String getRedirectUri(HttpServletRequest req) throws ServletException, IOException {
GenericUrl url = new GenericUrl(req.getRequestURL().toString());
url.setRawPath("/oauth2callback");
return url.build();
}
@Override
protected AuthorizationCodeFlow initializeFlow() throws IOException {
return new GoogleAuthorizationCodeFlow.Builder(
new NetHttpTransport(), JacksonFactory.getDefaultInstance(),
"[[ENTER YOUR CLIENT ID]]", "[[ENTER YOUR CLIENT SECRET]]",
Collections.singleton(CalendarScopes.CALENDAR)).setDataStoreFactory(
DATA_STORE_FACTORY).setAccessType("offline").build();
}
@Override
protected String getUserId(HttpServletRequest req) throws ServletException, IOException {
// return user ID
}
}
public class CalendarServletCallbackSample extends AbstractAuthorizationCodeCallbackServlet {
@Override
protected void onSuccess(HttpServletRequest req, HttpServletResponse resp, Credential credential)
throws ServletException, IOException {
resp.sendRedirect("/");
}
@Override
protected void onError(
HttpServletRequest req, HttpServletResponse resp, AuthorizationCodeResponseUrl errorResponse)
throws ServletException, IOException {
// handle error
}
@Override
protected String getRedirectUri(HttpServletRequest req) throws ServletException, IOException {
GenericUrl url = new GenericUrl(req.getRequestURL().toString());
url.setRawPath("/oauth2callback");
return url.build();
}
@Override
protected AuthorizationCodeFlow initializeFlow() throws IOException {
return new GoogleAuthorizationCodeFlow.Builder(
new NetHttpTransport(), JacksonFactory.getDefaultInstance()
"[[ENTER YOUR CLIENT ID]]", "[[ENTER YOUR CLIENT SECRET]]",
Collections.singleton(CalendarScopes.CALENDAR)).setDataStoreFactory(
DATA_STORE_FACTORY).setAccessType("offline").build();
}
@Override
protected String getUserId(HttpServletRequest req) throws ServletException, IOException {
// return user ID
}
}
根據我從您的問題和評論中了解到的情況,您希望實現以下目標:
使用您當前的代碼從您的應用程序發送 email 消息,這些消息已經在您的本地計算機上運行,現在您也想在 web 服務器中執行此操作。
考慮到您是唯一需要授權應用程序的用戶,您可以使用服務帳戶實現您的目標。
服務帳戶最適合像您這樣的應用程序,您不需要用戶授權,而是需要您的帳戶授權,例如自動發送電子郵件。
這些類型的帳戶屬於您的應用程序而不是單個用戶,並且可以發出 API 請求,而無需通過 UI 授權。 文檔中的本指南將引導您逐步了解如何使用服務帳戶設置授權。
此外,請記住,為了讓服務帳戶以您的名義執行請求(因為您需要用戶發送電子郵件),您應該使用域范圍的授權,以便服務帳戶可以以用戶的名義發出此類請求. 另請注意,您需要一個 Google Workspace 帳戶來實施域范圍的授權(如果您沒有,請告訴我,因為我可以提出解決方法) 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.