Google sheets 无法使用google电子表格api从电子表格检索工作表
我收到错误com.google.gdata.client.GoogleService$SessionExpiredException:当我试图从特定电子表格检索工作表时,未经授权。我使用的代码与中给出的代码相同,但仍然出现了错误 代码如下: 公共类电子表格快速入门{Google sheets 无法使用google电子表格api从电子表格检索工作表,google-sheets,google-spreadsheet-api,Google Sheets,Google Spreadsheet Api,我收到错误com.google.gdata.client.GoogleService$SessionExpiredException:当我试图从特定电子表格检索工作表时,未经授权。我使用的代码与中给出的代码相同,但仍然出现了错误 代码如下: 公共类电子表格快速入门{ private static final String SCOPE = "https://spreadsheets.google.com/feeds/spreadsheets/private/full"; private
private static final String SCOPE = "https://spreadsheets.google.com/feeds/spreadsheets/private/full";
private static final String APP_NAME = "S.A.R.A.H";
authorized user.
private static final String USER = "me";
private static final String CLIENT_SECRET_PATH = "C://Users/ahrashid.contractor/Downloads/client_secret_461052169747-ffbpdvcbmu822r2vg2bhrn4ae3lrjri8.apps.googleusercontent.com.json";
private static GoogleClientSecrets clientSecrets;
public static void main(String[] args) throws FileNotFoundException, IOException, ServiceException {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
clientSecrets = GoogleClientSecrets.load(jsonFactory, new FileReader(CLIENT_SECRET_PATH));
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, clientSecrets, Arrays.asList(SCOPE))
.setAccessType("offline")
.setApprovalPrompt("force").build();
String url = flow.newAuthorizationUrl().setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI)
.build();
System.out.println("Please open the following URL in your browser then type"
+ " the authorization code:\n" + url);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String code = br.readLine();
// Generate Credential using retrieved code.
GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI).execute();
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory).setClientSecrets(clientSecrets).build().setFromTokenResponse(response);
SpreadsheetService service = new SpreadsheetService(APP_NAME);
service.setOAuth2Credentials(credential);
SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL,SpreadsheetFeed.class);
List<SpreadsheetEntry> spreadsheets = feed.getEntries();
if (spreadsheets.size() == 0) {
// TODO: There were no spreadsheets, act accordingly.
}
// TODO: Choose a spreadsheet more intelligently based on your
// app's needs.
SpreadsheetEntry spreadsheet = spreadsheets.get(0);
System.out.println(spreadsheet.getTitle().getPlainText());
// Make a request to the API to fetch information about all
// worksheets in the spreadsheet.
/* Till here everything is working fine. I'm getting the exception in the below line */
List<WorksheetEntry> worksheets = spreadsheet.getWorksheets(); /* this line gives com.google.gdata.client.GoogleService$SessionExpiredException: Unauthorized exception" */
// Iterate through each worksheet in the spreadsheet.
for (WorksheetEntry worksheet : worksheets) {
// Get the worksheet's title, row count, and column count.
String title = worksheet.getTitle().getPlainText();
int rowCount = worksheet.getRowCount();
int colCount = worksheet.getColCount();
// Print the fetched information to the screen for this worksheet.
System.out.println("\t" + title + "- rows:" + rowCount + " cols: " + colCount);
}我在标头中设置令牌,而不是使用service.setOAuth2Credentialscredential-此外,代币在1小时后过期。