繁体   English   中英

在Android中通过GoogleAuthUtil从Google Drive API获取访问令牌

[英]Get Access Token From Google Drive API by GoogleAuthUtil in Android

访问Google云端硬盘API时,使用GoogleAuthUtil出现了一个奇怪的问题。

以下是我的代码,

    @Override
    protected String doInBackground(String... params){
        String accountName = params[0];
        //String scopes = "oauth2:profile email"; // This can work !
        String scopes = "oauth2:server:client_id:7666010xxxxx-dm0d37oxxxxxxxih8k5sm5g7tajetubk.apps.googleusercontent.com:api_scope:https://www.googleapis.com/auth/drive";
        String token = null;
        try{
            token = GoogleAuthUtil.getToken(getApplicationContext(), accountName, scopes);
        }
        catch (IOException e){
            Log.e(excpTAG, "IO Exception: " + e.getMessage());
        }
        catch (UserRecoverableAuthException e){
            startActivityForResult(e.getIntent(), REQ_SIGN_IN_REQUIRED);
        }
        catch (GoogleAuthException e)
        {
            Log.e(excpTAG, "GoogleAuthException: " + e.getMessage());
        }
        return token;
    }

在我的经验测试中,我可以通过将范围设置为来获取用户的Google个人资料/电子邮件的访问令牌

String scopes = "oauth2:profile email";

但是,当我要访问用户的Google云端硬盘时,我遵循了开发文档中的说明

https://developers.google.com/accounts/docs/CrossClientAuth

并将范围设置为

String scopes = "oauth2:server:client_id:766601xxxxxx-dm0dxxxxxxxxxxxxxxxx7tajetubk.apps.googleusercontent.com:api_scope:https://www.googleapis.com/auth/drive";

执行该应用程序时,Android-Studio中的Log cat始终提到发生了异常事件GoogleAuthException,并且事件消息为“未知”。

在Android中获取Google云端硬盘访问令牌的正确方法是什么? 非常感谢。

终于我找到了答案!

造成此问题的主要原因应该是Google API控制台Android-Studio之间的“ PACKAGE NAME和“ CERTIFICATE FINGERPRINT (SHA1)不一致

在某些情况下,不一致会导致授权异常错误。

如果Android-Studio中的“ PACKAGE NAME和“ CERTIFICATE FINGERPRINT (SHA1)Google API控制台中的“ PACKAGE NAME和“ CERTIFICATE FINGERPRINT (SHA1)不一致,则Google API不会简单地拒绝所有请求。 某些驱动器(如DriveScopes.Drive触发了异常错误,而其他诸如oauth2:profile email未触发。

在这种情况下,异常错误还会返回“ Unknown消息,从而使我们无法正确解决由某些范围引起的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM