代码之家  ›  专栏  ›  技术社区  ›  JSmith

在续订对google帐户的凭据和应用程序访问权限后授权作用域

  •  0
  • JSmith  · 技术社区  · 7 年前

    这是我的问题。

    我正在用JAVA中的GoogleAppEngine开发一个web应用程序。

    我在网上搜索了很多关于我的问题的不同答案,给出了各种各样的解决方案,但没有一个对我有效,我仍然得到:

    {
      "code" : 403,
      "errors" : [ {
        "domain" : "global",
        "message" : "Insufficient Permission",
        "reason" : "insufficientPermissions"
      } ],
      "message" : "Insufficient Permission"
    }
    

    我正在尝试将一封邮件从我的帐户发送到其他电子邮件地址(作为测试从我发送到我)。

    我有两个凭证文件。

    1. 为了检索用户信息以确认通过Google登录,这里我使用openID连接URL,在那里我获得用户同意屏幕。
    2. 使用Sheets API和Gmail API作为“我”首先从中检索数据 电子表格和从 我的 电子邮件地址。如前所述,我已经验证了Sheets API,但无法让Gmail使用相同的凭据。

    编辑

    刚刚删除了我的工作表API凭据也删除了我的应用程序fto以链接到我的帐户,现在我无法获得弹出的同意屏幕。我得到:

    com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
    {
      "error" : "invalid_grant",
      "error_description" : "Token has been expired or revoked."
    }
    

    我正在使用此代码获取凭据

    private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        // Load client secrets.
        InputStreamReader in = new InputStreamReader(new FileInputStream(new File("WEB-INF/credentials.json")));
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, in);
    
        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
        return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    }
    

    但谷歌的文档显示:

    授权信息存储在文件系统中,因此

    在哪里可以删除存储的文件系统?

    非常感谢您的帮助

    1 回复  |  直到 7 年前
        1
  •  0
  •   JSmith    7 年前

    经过一个又一个小时的胡闹,我终于找到了一个无法解释的解决办法。

    我从中删除我的应用程序 this page

    然后将我的代码更改为

    return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    

    对此

    return new AuthorizationCodeInstalledApp(flow, receiver).authorize(null);
    

    然后,我在验证我的应用程序后,得到一个 NullPointerException 错误,因此我将同一行改回此

    return new AuthorizationCodeInstalledApp(flow, receiver).authorize("myemail@gmail.com");
    

    如果它不工作(我想这是缓存问题)

    对此

    return new AuthorizationCodeInstalledApp(flow, receiver).authorize("me");
    

    别问我为什么,但这确实奏效了。 欢迎提供更多关于这种行为的详细信息。

    推荐文章