如何提供谷歌访问令牌以检索用户电子表格列表?

时间:2014-07-07 11:47:09

标签: google-api google-sheets

用户已成功授权我的应用使用其数据。所以基本上我的应用程序有用户的访问令牌。现在我需要获取用户的电子表格列表。 有一个例子:

import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;

import java.io.IOException;
import java.net.*;
import java.util.*;

public class MySpreadsheetIntegration {
  public static void main(String[] args)
      throws AuthenticationException, MalformedURLException, IOException, ServiceException {

    SpreadsheetService service =
        new SpreadsheetService("MySpreadsheetIntegration-v1");

    // TODO: Authorize the service object for a specific user (see other sections)

    // Define the URL to request.  This should never change.
    URL SPREADSHEET_FEED_URL = new URL(
        "https://spreadsheets.google.com/feeds/spreadsheets/private/full");

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

    // Iterate through all of the spreadsheets returned
    for (SpreadsheetEntry spreadsheet : spreadsheets) {
      // Print the title of this spreadsheet to the screen
      System.out.println(spreadsheet.getTitle().getPlainText());
    }
  }
}

如何提供acsess令牌?

1 个答案:

答案 0 :(得分:1)

我添加了这个代码段

service.setOAuth2Credentials(new Credential(BearerToken
            .authorizationHeaderAccessMethod())
            .setFromTokenResponse(new TokenResponse().setAccessToken(accessToken)));

并请求范围

https://spreadsheets.google.com/feeds