繁体   English   中英

在Google API Calendar V3 Java和OAUTH2上需要帮助

[英]Need help on Google API Calendar V3 Java and OAUTH2

我正在尝试使用Google Calendar APi V3(Java)处理我的Google议程。 但是,我对此和OAUTH2都很陌生..然后我在其中搜索了示例,然后在这里找到了一个示例: Google Calendar API V3 Java:无法为Calendars使用“ primary”:get这是代码:

   import java.io.IOException;
import java.util.Collections;
import java.util.Scanner;
import java.util.Set;

import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
import com.google.api.client.auth.oauth2.AuthorizationCodeRequestUrl;
import com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest;
import com.google.api.client.auth.oauth2.TokenResponse;
import com.google.api.client.extensions.auth.helpers.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.Calendar.CalendarList;
import com.google.api.services.calendar.CalendarScopes;
import com.google.api.services.calendar.model.CalendarListEntry;


public class App {
    public static void main(String[] args) throws IOException{
        //Two globals that will be used in each step.
        HttpTransport httpTransport = new NetHttpTransport();
        JsonFactory jsonFactory = new JacksonFactory();

    //Create the authorization code flow manager
    Set<String> scope = Collections.singleton(CalendarScopes.CALENDAR);
    String clientId = "xxxxxx.apps.googleusercontent.com";
    String clientSecret = "xxxxxxxxxxx";

    //Use a factory pattern to create the code flow
    AuthorizationCodeFlow.Builder codeFlowBuilder = 
            new GoogleAuthorizationCodeFlow.Builder(
                    httpTransport, 
                    jsonFactory, 
                    clientId, 
                    clientSecret, 
                    scope
            );
    AuthorizationCodeFlow codeFlow = codeFlowBuilder.build();

    //set the code flow to use a dummy user
    //in a servlet, this could be the session id
    String userId = "ipeech";

    //"redirect" to the authentication url
    String redirectUri = "https://www.example.com/oauth2callback";
    AuthorizationCodeRequestUrl authorizationUrl = codeFlow.newAuthorizationUrl();
    authorizationUrl.setRedirectUri(redirectUri);
    System.out.println("Go to the following address:");
    System.out.println(authorizationUrl);

    //use the code that is returned as a url parameter
    //to request an authorization token
    System.out.println("What is the 'code' url parameter?");
   String code = new Scanner(System.in).nextLine();

    AuthorizationCodeTokenRequest tokenRequest = codeFlow.newTokenRequest(code);
    tokenRequest.setRedirectUri(redirectUri);
    TokenResponse tokenResponse = tokenRequest.execute();

    //Now, with the token and user id, we have credentials
    com.google.api.client.auth.oauth2.Credential credential = codeFlow.createAndStoreCredential(tokenResponse, userId);

    //Credentials may be used to initialize http requests
    HttpRequestInitializer initializer = credential;
    //and thus are used to initialize the calendar service
    Calendar.Builder serviceBuilder = new Calendar.Builder(
            httpTransport, jsonFactory, initializer);
    serviceBuilder.setApplicationName("Example");
    Calendar calendar = serviceBuilder.build();

    //get some data

    String calendarID = "xxxxxxxxxxx";
    getCalendarListSummary(calendarID,calendar);
    getAllCalendarListSummary(calendar);
    //getCalendarSummary(calendarID,calendar);
}

public static void getCalendarListSummary(String calendarID, Calendar calendar) throws IOException{
    CalendarListEntry calendarListEntry = calendar.calendarList().get(calendarID).execute();
    System.out.println(calendarListEntry.getSummary());
}

public static void getAllCalendarListSummary (Calendar calendar) throws IOException{
    Calendar.CalendarList.List listRequest = calendar.calendarList().list();
    com.google.api.services.calendar.model.CalendarList feed = listRequest.execute();
    for(CalendarListEntry entry:feed.getItems()){
        System.out.println("ID: " + entry.getId());
        System.out.println("Summary: " + entry.getSummary());
    }
}

当我启动程序时,它要求我提供授权代码(“'code'url参数是什么?”),但是我不知道在哪里可以找到它。

在此示例中,有一部分显示为“转到以下地址:”,您必须复制该URL,将其粘贴到浏览器中,然后您将收到授权代码。 复制该代码并将其粘贴到“'code'url参数是什么?”之后 然后按“ Enter”,这样程序就可以继续。

这是一个基本示例,并且说明了为什么以这种方式完成OAuth 2流程。

是Google日历Java程序的完整示例。 我建议首先了解OAuth 2的工作原理,如何在开发人员控制台中创建项目以及如何为这些项目创建凭据。 然后,将更容易理解和使用完整的示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM