使用服务密钥进行 SpreadsheetService 身份验证并在尝试获取提要时出现内部服务器错误

Using service key for SpreadsheetService authentication and getting Internal Server Error when trying to get the feed

我正在使用 SpreadsheetService 并尝试从电子表格中获取提要 URL (https://spreadsheets.google.com/feeds/spreadsheets/private/full).

我正在使用服务密钥进行身份验证。似乎身份验证有效,但当我尝试获取提要时出现错误:

ServiceException (com.google.gdata.util.ServiceException: Internal Server Error).

这是认证方式:

public static Credential authenticateWithServiceKey(GoogleApiProperties properties) throws IOException {
    GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("src/main/resources/prime-moment-274810-940d01c0244d.json"))
            .createScoped(Collections.singleton(properties.getScope()));

    return credential;
}

这就是我创建服务并使用上述方法中的凭据对象进行身份验证的方式:

public void init(GoogleApiProperties properties) throws IOException, GeneralSecurityException {
    Credential credential = GoogleAuthentication.authenticateWithServiceKey(properties);
    service = new SpreadsheetService(properties.getApplicationName());
    service.setOAuth2Credentials(credential);
}

这是我试图获取电子表格列表的地方:

public List<String> getContentList() throws IOException, ServiceException {
    // Define the URL to request.  This should never change.
    URL SPREADSHEET_FEED_URL = new URL(SPREADSHEETS_URL);

    // Make a request to the API and get all spreadsheets.
    SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);

    // Return a list of spreadsheet names
    return feed.getEntries().stream()
            .map(entry -> entry.getTitle().getPlainText())
            .collect(Collectors.toList());
}

service.getFeed 调用之后出现异常。

当我使用常规方法获取 OAuth2.0 凭据时,它工作正常,但需要手动干预(通过 [ 接受连接) =36=]页)。

错误不在于凭据,而在于执行。看来你一切都好,因为它是服务器错误。

虽然 Sheets API v3 正在 deprecated on Sep 2020 so I would recommend using Sheets API v4 method: get 获取电子表格的提要。

如果您转到表格 API v4 Migrate from previous API -> Retrieve sheet metadata 您会发现:

If you only want to read the sheet properties, set the includeGridData query parameter to false to prevent the inclusion of the spreadsheet cell data.

所以按照 Method: spreadsheets.get 的例子可以解决你的问题。

这是我使用的方法的简化版本:

public static void main(String... args) throws IOException, GeneralSecurityException {
        // Build a new authorized API client service.
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        final String spreadsheetId = "YOUR SPREADSHEET ID";
        final String range = "Class Data!A2:E";
        Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                .setApplicationName(APPLICATION_NAME)
                .build();
        Sheets.Spreadsheets.Get request = service.spreadsheets().get(spreadsheetId);
        request.setIncludeGridData(false);

        //Sheets.Spreadsheets response = request.execute();

        // TODO: Change code below to process the `response` object:
        System.out.println(request.execute());
    }

编辑

要列出您必须通过云端硬盘完成的电子表格 API。

在这种情况下,通过方法 Files: List 和查询或 q 参数,输入电子表格 MIME 类型:mimeType = 'application/vnd.google-apps.spreadsheet'

您可以在 quickstart

上查看如何使用文件:列表