无需身份验证令牌即可访问Google电子表格API

时间:2017-05-18 10:25:20

标签: google-sheets google-spreadsheet-api google-authentication google-developers-console

我创建了Google电子表格,并为所有人提供了编辑权限(即使没有登录也可以编辑)。

这是link。我想使用Google Spreadsheet API更新此表。但我收到了错误。我的要求是即使没有访问凭证也要通过API更新工作表。

enter image description here

4 个答案:

答案 0 :(得分:6)

可以在没有OAuthAPI Keys的情况下写入电子表格。您需要使用Service Account Keys

这是我为Node.js环境所做的。

  1. https://console.cloud.google.com/apis/credentials获取服务帐户密钥(您也可以在此处限制此密钥允许使用的内容)
    1. 创建时,请务必点击Furnish a new private key
    2. 当它询问您如何下载密钥时选择JSON
  2. 您刚刚生成的service account key包含client_email
    1. 转到谷歌电子表格并允许此client_email拥有此文档的写入权限
  3. 使用以下代码进行身份验证

    let jwtClient = new google.auth.JWT(client_email, null, private_key, [ "https://www.googleapis.com/auth/spreadsheets", ]); //authenticate request jwtClient.authorize(function(err, tokens) { // at this point the authentication is done you can now use `jwtClient` // to read or write to the spreadsheet });

  4. client_emailprivate_keyservice account key

    的一部分

    可在此处找到更详细的描述。 http://isd-soft.com/tech_blog/accessing-google-apis-using-service-account-node-js/此外,所有功劳都归功于此页面。

答案 1 :(得分:3)

您需要authorized to make such requests

  

您的应用程序发送给Google表格API的每个请求都需要   向Google确定您的申请。有两种方法可以识别   您的应用程序:使用OAuth 2.0令牌(也授权   请求)和/或使用应用程序的API密钥。这是怎么回事   确定使用哪些选项:

     

如果请求需要授权(例如请求   个人的私人数据),然后应用程序必须提供OAuth   带有请求的2.0令牌。应用程序也可以提供API密钥,但它不是必须的。如果请求不是必需的   授权(如公共数据请求),然后   应用程序必须提供API密钥或OAuth 2.0令牌,或   无论什么选择对你来说都是最方便的。

那就是它。没有旁路授权。

答案 2 :(得分:0)

最后深入挖掘并找到了答案。任何形式的写作,甚至是可公开编辑的工作表,都需要OAuth流程:

https://issuetracker.google.com/issues/36755576#comment3

答案 3 :(得分:0)

JürgenBrandstetter的回答完全正确。使用Postman,无需使用OAuth令牌即可成功(我需要我的个人API密钥和服务帐户)-我已写到新工作表中(实际上,我通过两个步骤执行了batchUpdate操作,首先创建一个新工作表,然后然后在上面粘贴数据)。我按照here的说明创建了一个服务帐户,下载了凭证JSON,并用它创建并签名了一个JWT字符串,以后将其用作Bearer。

这是获取JWT字符串的Java代码:

df['new_enc_id'] = (df['enc_id'].astype(str) + 
                    (df.groupby('person_id').cumcount()+1).astype(str))

所需依赖项:com.auth0:java-jwt和com.google.auth:google-auth-library-oauth2-http。

以下是使用上面生成的JWT字符串的curl:

    private static String getSignedJWT() throws IOException {
        InputStream in = YourClass.class.getResourceAsStream("/downloaded-service-account-creds.json");
        if (in == null) {
            throw new FileNotFoundException("Resource not found");
        }
        ServiceAccountCredentials serviceAccountCredentials = ServiceAccountCredentials.fromStream(in);
        GoogleCredentials googleCredentials = serviceAccountCredentials
                .createScoped(Collections.singletonList(SheetsScopes.SPREADSHEETS));

        PrivateKey privateKey = serviceAccountCredentials.getPrivateKey();
        String privateKeyId = serviceAccountCredentials.getPrivateKeyId();

        long now = System.currentTimeMillis();

        Algorithm algorithm = Algorithm.RSA256(null, (RSAPrivateKey)privateKey);
        String signedJwt = JWT.create()
                .withKeyId(privateKeyId)
                .withIssuer(serviceAccountCredentials.getClientEmail())
                .withSubject(serviceAccountCredentials.getClientEmail())
                .withAudience("https://sheets.googleapis.com/")
                .withIssuedAt(new Date(now))
                .withExpiresAt(new Date(now + 3600 * 1000L))
                .sign(algorithm);

        return signedJwt;
    }
相关问题