如何在允许用户使用Google应用的同时获取访问和刷新令牌

时间:2017-04-21 17:46:59

标签: google-apps google-apps-marketplace gsuite

我创建了一个谷歌应用程序,并在谷歌应用程序市场sdk api填写了所有必需的细节,并添加了我所需要的范围。并在chrome开发人员仪表板中创建了一个webapp并提交了应用程序以进行测试。当第一次从市场安装应用程序时,它获得了用户对我在谷歌应用程序市场sdk api中添加的范围的许可。单击允许后,它将只安装应用程序。在这里,如何在用户点击允许的情况下检索Accesstoken并刷新令牌或授权代码?

我使用此api调用手动获取Auth代码:

“”https://accounts.google.com/o/oauth2/auth?client_id=CLIENTID &redirect_uri=REDIRECT_URI&scope=email+profile+https://www.googleapis.com/auth/drive&response_type=code&access_type=offline “”

在重定向的uri中,使用此api调用收集访问和ref令牌:

String accessTokenUrl = "https://accounts.google.com/o/oauth2/token";
        String accessTokenUrlParameters = "client_id=" + clientId + "&client_secret=" + clientSecret + "&redirect_uri="
                + redirectUri + "&grant_type=authorization_code&code=" + code;
        String accessToken = null;
        String refreshToken = null;
        try {
            HttpClient hc = new HttpClient();
            hc.setURL(accessTokenUrl);
            hc.setHeader("Content-Length", "" + Integer.toString(accessTokenUrlParameters.getBytes().length));
            hc.doPost("application/x-www-form-urlencoded; charset=UTF-8");

}

在这种类型中,我同时获得访问权限和刷新令牌,但在第一次安装应用程序时如何使用该权限和范围获取访问令牌。安装应用程序时的同意屏幕有这样的URL。

https://accounts.google.com/o/oauth2/auth?client_id=1234567890-1od573nk87eq712l7suam1hu9upa8tm2.apps.googleusercontent.com&origin=https%3A%2F%2Fapis.google.com&authuser=0&login_hint=abcdefghijk@gmail.com&response_type=token&redirect_uri=postmessage&hl=en&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile

但它没有重定向uri和所有。如何在允许此同意屏幕的同时获取访问令牌。

0 个答案:

没有答案