oauth2.0 从流中阅读凭据时出错,未指定“类型”字段

1hdlvixo  于 2023-08-02  发布在  其他
关注(0)|答案(1)|浏览(96)

我正在开发一个Java应用程序,它使用GoogleAuth库通过API进行身份验证。我已经从Google Cloud Console下载了我的OAuth2凭据的credentials.json文件,我正在将此文件加载到我的应用程序中,如下所示:

InputStream in = GoogleCalendarExample.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
GoogleCredentials credentials = GoogleCredentials.fromStream(in).createScoped(SCOPES);

字符串
然而,当我尝试运行我的应用程序时,我得到以下错误:

Exception in thread "main" java.io.IOException: Error reading credentials from stream, 'type' field not specified.
        at com.google.auth.oauth2.GoogleCredentials.fromStream(GoogleCredentials.java:170)
        at com.google.auth.oauth2.GoogleCredentials.fromStream(GoogleCredentials.java:143)
        at pl.szylak.googleservices.GoogleCalendarExample.main(GoogleCalendarExample.java:32)


我检查了我的credentials.json文件,它不包括type字段。相反,它包含一个installed对象,其中包含client_idclient_secret和其他详细信息:

{
  "installed": {
    "client_id": "CLIENT_ID.apps.googleusercontent.com",
    "project_id": "PROJECT_ID",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "CLIENT_SECRET",
    "redirect_uris": ["urn:ietf:wg:oauth:2.0:oob", "http://localhost"]
  }
}


我已经了解到Google Auth库应该能够毫无问题地处理这种类型的凭据文件,所以我不确定为什么我会看到这个错误。有没有人遇到过这个问题,有没有人知道如何解决?
我已经更改了credentials.json中占位符的值。
这是我的简单的java代码,用于向我的日历添加一些事件。

package pl.szylak.googleservices;

import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.DateTime;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.CalendarScopes;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.EventDateTime;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.util.Collections;

public class GoogleCalendarExample {
private static final String APPLICATION_NAME = "Google Calendar API Java Quickstart";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";
private static final String CREDENTIALS_FILE_PATH = "/resources/credentials.json";
private static final java.util.List\<String\> SCOPES = Collections.singletonList(CalendarScopes.CALENDAR);

    private static com.google.api.services.calendar.Calendar service;
    
    public static void main(String... args) throws IOException, GeneralSecurityException {
        // Build a new authorized API client service.
        final com.google.api.client.http.javanet.NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        InputStream in = GoogleCalendarExample.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        GoogleCredentials credentials = GoogleCredentials.fromStream(in).createScoped(SCOPES);
        service = new com.google.api.services.calendar.Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, new HttpCredentialsAdapter(credentials))
                .setApplicationName(APPLICATION_NAME)
                .build();
    
        // Call the Google Calendar API and add an event.
        addEvent();
    }
    
    private static void addEvent() throws IOException {
        Event event = new Event()
                .setSummary("Google I/O 2025")
                .setLocation("800 Howard St., San Francisco, CA 94103")
                .setDescription("A chance to learn more about Google's developer products.");
    
        EventDateTime start = new EventDateTime()
                .setDateTime(new DateTime("2025-05-28T09:00:00-07:00"))
                .setTimeZone("America/Los_Angeles");
        event.setStart(start);
    
        EventDateTime end = new EventDateTime()
                .setDateTime(new DateTime("2025-05-28T17:00:00-07:00"))
                .setTimeZone("America/Los_Angeles");
        event.setEnd(end);
    
        String calendarId = "primary";
        event = service.events().insert(calendarId, event).execute();
        System.out.printf("Event created: %s\n", event.getHtmlLink());
    }

}

aor9mmx1

aor9mmx11#

简而言之,你错过了“类型”:“service_account”,
由于上面的代码试图从一个文件(从-“/resources/credentials.json”)中读取凭证,但它无法在凭证文件中找到所需的'type'字段,因此还交叉检查凭证文件位于项目的“resources”目录中。

相关问题