我在使用Google Drive API的5分钟快速入门体验时遇到了困难,所有这些都是为了将我对Google Spreadsheets的程序化使用升级为符合OAuth2标准(是的,我知道,我是一个散步者) 。
我现在正处于简单地尝试列出我在云端硬盘上的电子表格的地步,我什么也没得到 - 没有数据,也没有错误。驾驶似乎很开心,但我不是。
非常感谢任何帮助...
以下是详细信息:
是的,我的Google云端硬盘中有电子表格(多个)。当我通过网络浏览器登录时,我会在https://spreadsheets.google.com/feeds/spreadsheets/private/full
我为已安装的应用程序创建了Google凭据。以编程方式上传云端硬盘文档的代码(来自Google"快速入门")运行良好。
以下代码运行得很好,但声称我没有电子表格。我很困惑为什么。
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.drive.DriveScopes;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
public class Spread {
public static void main(String[] args) throws Exception {
ArrayList scopes = new ArrayList();
scopes.add(0, DriveScopes.DRIVE);
scopes.add(1, "https://spreadsheets.google.com/feeds");
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(new NetHttpTransport())
.setJsonFactory(new JacksonFactory())
.setServiceAccountId(
"my-service-email-address@developer.gserviceaccount.com")
.setServiceAccountPrivateKeyFromP12File(new File("/path/to/my/P12/file"))
.setServiceAccountScopes(scopes).build();
credential.refreshToken();
SpreadsheetService service = new SpreadsheetService("tmp");
service.setOAuth2Credentials(credential);
// 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 spreadsheets = feed.getEntries();
// Iterate through all of the spreadsheets returned
int i = 0;
for (SpreadsheetEntry spreadsheet : spreadsheets) {
++i;
System.out.println(spreadsheet.getId());
}
System.out.println("Done " + i);
}
}
运行此代码的最终结果是"完成,找到0张。"
帮助!