Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 代表用户创建谷歌日历_Java_Spring Boot_Google Api_Google Calendar Api_Google Api Java Client - Fatal编程技术网

Java 代表用户创建谷歌日历

Java 代表用户创建谷歌日历,java,spring-boot,google-api,google-calendar-api,google-api-java-client,Java,Spring Boot,Google Api,Google Calendar Api,Google Api Java Client,我正在开发一个JavaSpringBoot后端应用程序。 其中我有多个用户,我需要为他们创建日历事件并使用他们的gmail id发送邮件。我只能使用单个用户的凭据来验证google api 有没有什么方法可以让我使用客户端的gmail id在邮件上创建日历事件,而不必每次使用google api都从UI获得客户端的同意 另外,如果有一种方法可以通过身份验证使用google api而不必与UI交互,请提供建议。google日历数据是私人用户数据,为了在用户日历上创建偶数,您需要拥有该日历的用户的许

我正在开发一个JavaSpringBoot后端应用程序。 其中我有多个用户,我需要为他们创建日历事件并使用他们的gmail id发送邮件。我只能使用单个用户的凭据来验证google api

有没有什么方法可以让我使用客户端的gmail id在邮件上创建日历事件,而不必每次使用google api都从UI获得客户端的同意


另外,如果有一种方法可以通过身份验证使用google api而不必与UI交互,请提供建议。

google日历数据是私人用户数据,为了在用户日历上创建偶数,您需要拥有该日历的用户的许可

为此,我们使用了Oauth2,通过Oauth2,您的应用程序将请求用户访问这些数据的权限,授权服务器将向您返回一个令牌,授予您代表用户访问api的权限

如果所有者未授予您访问私人用户数据的权限,则无法访问该数据

每次使用google api时,无需征得用户界面的同意

现在,您可以做的一件事是请求用户的脱机访问,如果用户授予您脱机访问权限,您将获得一个刷新令牌,您可以稍后使用该令牌请求新的访问令牌。使用访问令牌,您可以对用户日历进行更改,而无需用户实际在那里运行您的应用程序

用户存储的数据存储目录 这段代码应该很接近,它与google analytics的示例有所不同,但应该向您展示如何使用DATA_STORE_DIR来存储用户凭据以供以后使用

/**
 * A simple example of how to access the Google calendar API.
 */
public class HelloCalendar {
  // Path to client_secrets.json file downloaded from the Developer's Console.
  // The path is relative to HelloCalendar.java.
  private static final String CLIENT_SECRET_JSON_RESOURCE = "client_secrets.json";

  // The directory where the user's credentials will be stored.
  private static final File DATA_STORE_DIR = new File(
      System.getProperty("user.home"), ".store/hello_calendar");

  private static final String APPLICATION_NAME = "Hello Calendar";
  private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
  private static NetHttpTransport httpTransport;
  private static FileDataStoreFactory dataStoreFactory;

  public static void main(String[] args) {
    try {
      Calendar service = initializeCalendar();

      // do stuff here 
      printResponse(response);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }


  /**
   * Initializes an authorized calendar service object.
   *
   * @return The Calendar service object.
   * @throws IOException
   * @throws GeneralSecurityException
   */
  private static Calendar initializeCalendar() throws GeneralSecurityException, IOException {

    httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);

    // Load client secrets.
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
        new InputStreamReader(Calendar.class
            .getResourceAsStream(CLIENT_SECRET_JSON_RESOURCE)));

    // Set up authorization code flow for all authorization scopes.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow
        .Builder(httpTransport, JSON_FACTORY, clientSecrets,
            CalendarScopes.all()).setDataStoreFactory(dataStoreFactory)
        .build();

    // Authorize.
    Credential credential = new AuthorizationCodeInstalledApp(flow,
        new LocalServerReceiver()).authorize("THISISTHEUSERNAME");
    // Construct the Analytics Reporting service object.
    return new Calendar.Builder(httpTransport, JSON_FACTORY, credential)
        .setApplicationName(APPLICATION_NAME).build();
  }

您的意思是像API中那样进行身份验证吗?或者你的意思是不需要用户登录就可以对用户数据进行操作?我想代表用户添加日历事件,并使用用户的gmail id发送邮件,但这是可以实现的。我更改了不同用户的目录路径和凭据,这对多个用户都很好。非常感谢。