部署的web应用程序未生成授权令牌

sf6xfgos  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(378)

我正在尝试使用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文件。
当我在dev server中部署时,我无法生成访问和刷新令牌保存的文件。
你能帮帮我吗。

svmlkihl

svmlkihl1#

authorizationcodeinstalledapp oauth 2.0已安装持久化最终用户凭据的java应用程序的授权代码流。
你不能把它部署到服务器上,因为它会打开服务器上的授权窗口。
对于一个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
  }
}
6ss1mwsb

6ss1mwsb2#

根据我对你的问题和评论的理解,你希望实现以下目标:
使用当前代码从应用程序发送电子邮件,该应用程序已在本地计算机上运行,现在也要在web服务器上运行。
考虑到您是唯一需要授权应用程序的用户,您可以使用服务帐户实现您的目标。
服务帐户最适用于像您这样的应用程序,您不需要用户的授权,而是需要您的帐户来自动发送电子邮件。
这些类型的帐户属于您的应用程序,而不是单个用户,可以在不需要通过ui授权的情况下发出api请求。本文档中的指南将指导您逐步了解如何使用服务帐户设置授权。
此外,请记住,为了让服务帐户以您的名义执行请求(因为您需要用户发送电子邮件),您应该使用域范围的授权,以便服务帐户可以以用户的名义发出此类请求。另外请注意,您需要一个googleworkspace帐户来实现域范围的授权(如果您没有,请告诉我,因为我可以提出一个解决方法)。

相关问题