问题描述:

I'm getting the error "com.google.gdata.client.GoogleService$SessionExpiredException: Unauthorized" when I try to retrieve the worksheets from a particular spreadsheet. I'm using the same code that has been given in "https://developers.google.com/google-apps/spreadsheets/#audience" but still I'm getting the error.

Below is the code:

public class 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);

}

相关阅读:
Top