android - Android 上的 Google Spreadsheet API 无效 token

标签 android google-drive-api google-sheets

我正在尝试创建一个 Android 应用程序,它在谷歌驱动器上创建一个电子表格,然后将数据保存到其中。驱动器部分工作正常,但由于某种原因,当我尝试使用我为谷歌驱动器获得的身份验证时,大概是电子表格 api(范围 https://spreadsheets.google.com/feeds/ 与电子表格 api 我得到“com.google.gdata.util.AuthenticationException: token 无效”

这是我的相关代码

    private static final String[] SCOPES = { DriveScopes.DRIVE, "https://spreadsheets.google.com/feeds/" };

    credential = GoogleAccountCredential.usingOAuth2(
            getApplicationContext(), Arrays.asList(SCOPES))
            .setBackOff(new ExponentialBackOff())
            .setSelectedAccountName(googleUserName);

    driveService = new com.google.api.services.drive.Drive.Builder(
            transport, jsonFactory, credential)
            .setApplicationName(getString(R.string.app_name))
            .build();

    //Calls to driveService work as expected
    //But this returns the invalid token exception
    SpreadSheetService service = new SpreadsheetService("AppSheetservice");

    final URL SPREADSHEET_FEED_URL = new URL(
            "https://spreadsheets.google.com/feeds/spreadsheets/private/full");

    service.setUserToken(saleActivity.credential.getToken());
    //This line throws the invalid token error
    feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);

最佳答案

我终于弄明白问题是你需要打电话

service.setAuthSubToken(credential.getToken());

代替

service.setUserToken(credential.getToken());

关于android - Android 上的 Google Spreadsheet API 无效 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31222106/

相关文章:

google-drive-api - Google Drive API 响应很慢

google-sheets - 如何比较两个(不相同)选项卡(或工作表)中特定列中的差异

javascript - 尝试将 Stripe API key 添加到 Google Apps 脚本中的 UrlFetchApp 时收到 401 'truncated server' 错误

android - 单击 RecyclerView 后在主要 Activity 中更新 TextView

android - 我怎样才能返回谷歌地图方向的距离?

java - 如何在java中对齐换行符?

python - Google Slides API - 如何获取 masterTemplate ID?

google-drive-api - Google Drive API、Oauth 和服务帐号

json - 在 Google 表格中解析 JSON

java - 使用硬件设备的 Android 开发人员,测试应用程序无法运行