com.google.gdata.client.GoogleService $ SessionExpiredException:未经授权

时间:2015-10-12 17:40:24

标签: google-sheets

我正在尝试从com.google.gdata.data.spreadsheet.SpreadsheetEntry获取工作表。

身份验证有效:

    GoogleCredential credential = authorize();
    SpreadsheetService service = new SpreadsheetService("My Project");
    service.setProtocolVersion(SpreadsheetService.Versions.V3);
    service.setOAuth2Credentials(credential);

我可以获取电子表格条目:

    URL SPREADSHEET_FEED_URL = new URL( "https://spreadsheets.google.com/feeds/spreadsheets/private/full");
    SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
    List<SpreadsheetEntry> spreadsheets = feed.getEntries();

但是一旦我调用spreadsheet.getWorksheets()方法,我就会收到以下错误:

com.google.gdata.client.GoogleService $ SessionExpiredException:Unauthorized

我的授权方法如下:

    File p12 = new File("src/main/resources/My Project-e81707237c5b.p12");
    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();
    String [] SCOPESArray= {
            "https://spreadsheets.google.com/feeds/spreadsheets/private/full"
    };
    final List SCOPES = Arrays.asList(SCOPESArray);
    GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
            .setServiceAccountId("727225752132-rnjnr63tk6ause0b2lotepig0mao837p@developer.gserviceaccount.com")
            .setServiceAccountScopes(SCOPES)
            .setServiceAccountPrivateKeyFromP12File(p12)
            .build();

1 个答案:

答案 0 :(得分:0)

我错过了两个服务帐户范围。以下是必需的范围:

    String [] SCOPESArray= {
            "https://spreadsheets.google.com/feeds",
            "https://spreadsheets.google.com/feeds/spreadsheets/private/full",
            "https://docs.google.com/feeds"
    };