我正在开发一个Android应用程序,并通过以下教程添加了一个google sign in功能:https://developers.google.com/identity/sign-in/android/sign-in
现在用户登录了,我希望能够读取和写入他们的谷歌电子表格。我已经查看了电子表格API,并告诉您使用OAuth来授权请求:https://developers.google.com/google-apps/spreadsheets/?hl=en
从登录页面教程:
Google Sign-In按钮验证用户并管理OAuth 2.0流程,这简化了您与Google api的集成。
我正在尝试使用Google登录页面进行授权过程。我现在可以使用GoogleApiClient成功登录和退出我的Google帐户,所以我尝试添加以下代码来访问我的电子表格,当用户登录时我调用这些电子表格:
private class RetrieveFeedTask extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... urls) {
SpreadsheetService service = new SpreadsheetService("MySpreadsheetIntegration-v1");
try {
URL SPREADSHEET_FEED_URL = new URL(urls[0]);
SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
List<SpreadsheetEntry> spreadsheets = feed.getEntries();
for (SpreadsheetEntry spreadsheet : spreadsheets) {
System.out.println(spreadsheet.getTitle().getPlainText());
}
} catch (ServiceException | IOException e) {
e.printStackTrace();
}
return null;
}
}
我用这行来调用它:
new RetrieveFeedTask().execute("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
但是在服务上。我得到以下错误:
com.google.gdata.util.ParseException: Unrecognized content type:application/binary
at com.google.gdata.client.Service.parseResponseData(Service.java:2136)
at com.google.gdata.client.Service.parseResponseData(Service.java:2098)
at com.google.gdata.client.Service.getFeed(Service.java:1136)
at com.google.gdata.client.Service.getFeed(Service.java:998)
at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:645)
at com.google.gdata.client.Service.getFeed(Service.java:1017)
at com.zarwanhashem.ideatrackr.MainActivity$RetrieveFeedTask.doInBackground(MainActivity.java:320)
at com.zarwanhashem.ideatrackr.MainActivity$RetrieveFeedTask.doInBackground(MainActivity.java:312)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
我通过查看过去的问题找到的解决方案不起作用。其中之一就是服务他人。setUserCredentials(user, password),但我不知道如何获得该信息,因为我没有亲自实现OAuth部分。
我如何解决这个错误,并使用谷歌登录页面作为我的身份验证?
首先,我建议设置包含访问令牌的授权头。
String accountname = Plus.AccountApi.getAccountName(mGoogleApiClient);
String scope = "oauth2:" + Scopes.PLUS_LOGIN + " " + "https://spreadsheets.google.com/feeds"; // add more scopes here
String accessToken = GoogleAuthUtil.getToken(context, accountname, scope);
service.setHeader("Authorization", "Bearer " + accessToken);
另外,请注意,如果你想创建新的工作表,你需要添加更多的作用域:
此外,如果应用程序需要创建电子表格,或者以其他方式操作它们的元数据,那么应用程序必须还请求Google Drive API范围;请参阅Google中的"选择授权范围"参考API文档获取可用作用域的信息。
另一件要尝试的事情是使用setOAuth2Credentials方法,如下所述,无耻地从使用Google Spreadsheet API在Google drive中创建电子表格中窃取。
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
String [] scopesArray= {"https://spreadsheets.google.com/feeds", "https://docs.google.com/feeds"}; // add more scopes here
final List SCOPES = Arrays.asList(scopesArray);
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(SCOPES)
.setServiceAccountPrivateKeyFromP12File(SERVICE_ACCOUNT_PKCS12_FILE)
.build();
SpreadsheetService service = new SpreadsheetService("MySpreadsheetIntegration-v1");
service.setOAuth2Credentials(credential);
P12密钥文件请参见https://webapps.stackexchange.com/questions/58411/how-where-to-obtain-a-p12-key-file-from-the-google-developers-console
其他引用:https://developers.google.com/google-apps/spreadsheets/authorizehttps://developers.google.com/drive/web/scopeshttps://developers.google.com/android/reference/com/google/android/gms/common/api/Scope