【问题标题】:How to access google spreadsheet from android app without OAuth?如何在没有 OAuth 的情况下从 android 应用程序访问谷歌电子表格?
【发布时间】:2015-04-30 13:22:25
【问题描述】:

我正在使用 Google 表格 API 将我在 Google 文档中的 Google 电子表格连接到我的 android 应用程序。

我想连接一个公共电子表格作为我的应用程序的数据库。我想知道如何在没有 OAuth 的情况下做到这一点。我只希望拥有该应用程序的人能够访问/读取数据库。

我想知道如何做到这一点?

我已按照 Google 电子表格 API 页面的说明进行操作。

另外,以下网址如何帮助我访问我的公开 Google 电子表格? 以下网址(取自 google sheet api 页面)

https://spreadsheets.google.com/feeds/worksheets/key/private/full

我的公开 Google 电子表格:here

【问题讨论】:

标签: android google-oauth google-spreadsheet-api


【解决方案1】:

更新:这实际上需要在 Google Drive API 控制台中设置 OAuth 令牌,其中包含用于签署应用程序的密钥指纹和应用程序的包名称。

可以在没有任何生成的 OAuth 令牌或凭据的情况下使用手机用户的 Google 帐户,这可能会通过任何阻止访问电子表格的授权障碍:

首先,设置登录客户端:

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestScopes(new Scope("https://www.googleapis.com/auth/spreadsheets"))
        .requestEmail()
        .build();
    googleSignInClient = GoogleSignIn.getClient(this, gso);

如果用户尚未登录,则提示用户登录(已登录时提示登录将导致取消错误):

    if (GoogleSignIn.getLastSignedInAccount(this) == null) {
        startActivityForResult(googleSignInClient.getSignInIntent(), RC_SIGN_IN);
    } else {
        // use the already signed in account
        Account account = GoogleSignIn.getLastSignedInAccount(this).getAccount();
    }

如果提示用户登录,那么在该活动完成后:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RC_SIGN_IN) {
        try {
            Account account = GoogleSignIn.getSignedInAccountFromIntent(data).getResult(ApiException.class).getAccount();
        } catch (ApiException e) {
            Log.w("MyActivity", "signInResult: failed code=" + e.getStatusCode() + ", reason: " + GoogleSignInStatusCodes.getStatusCodeString(e.getStatusCode()), e);
        }
    }
}

然后在没有任何特殊 OAuth 令牌的情况下使用用户帐户使用工作表 API:

    GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, Collections.singleton("https://www.googleapis.com/auth/spreadsheets"));
    credential.setSelectedAccount(account);
    Sheets sheetsService = new Sheets.Builder(AndroidHttp.newCompatibleTransport(), JacksonFactory.getDefaultInstance(), credential).build();
    Spreadsheet response = null;
    try {
        response = sheetsService.spreadsheets().get("insert spreadsheet ID here").setRanges(Arrays.asList("'sheet name here'!A2:G30"))
                .setFields("sheets.data.rowData.values.effectiveValue").execute();
    } catch (UserRecoverableAuthIOException ex) {
        context.startActivity(ex.getIntent());
    } catch (IOException e) {
        Log.e(CLASS_NAME, "failure to get spreadsheet: " + e.getMessage(), e);
    }

使用用户的 OAuth 而不是生成任何令牌来访问 Google 表格的完整项目在这里:https://github.com/stephenkittelson/interviewsetter

【讨论】:

    【解决方案2】:

    使用Google SpreadSheet Library for Android by Prasanta 可以轻松访问表格数据。您可以在他的blog 上找到支持的功能列表。

    以 JSON 格式访问表格数据并在您的应用中对其进行解析。

    使用此 URL 访问 JSON:https://spreadsheets.google.com/tq?key=

    例如:https://spreadsheets.google.com/tq?key=1tJ64Y8hje0ui4ap9U33h3KWwpxT_-JuVMSZzxD2Er8k

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      • 1970-01-01
      相关资源
      最近更新 更多