当我尝试从特定电子表格中检索工作表时,收到错误“com.google.gdata.client.GoogleService $ SessionExpiredException:Unauthorized”。我使用的是“https://developers.google.com/google-apps/spreadsheets/#audience”中提供的相同代码,但我仍然收到错误。
以下是代码:
公共类SpreadsheetApiQuickstart {
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);
}