Google api 如何在没有回调的情况下将Google一次性授权代码交换为刷新令牌(intranet)?

Google api 如何在没有回调的情况下将Google一次性授权代码交换为刷新令牌(intranet)?,google-api,google-api-php-client,google-oauth,Google Api,Google Api Php Client,Google Oauth,我正在开发一个基于intranet的应用程序,我想使用Google服务。目前,我已经使用JavaScript客户端身份验证通过“登录网站”成功实现了Google身份验证。我的用户现在可以登录或使用他们的谷歌帐户注册 现在我想使用GoogleAPI创建并与我的用户共享GoogleSheet。这些文档将使用特定的Google帐户创建,然后与我的用户共享 这就是我希望使用此服务器幻灯片流获取一次性授权代码并将其交换为刷新令牌的原因: 此刷新令牌将存储在我的数据库中,允许我代表此脱机用户使用Goog

我正在开发一个基于intranet的应用程序,我想使用Google服务。目前,我已经使用JavaScript客户端身份验证通过“登录网站”成功实现了Google身份验证。我的用户现在可以登录或使用他们的谷歌帐户注册

现在我想使用GoogleAPI创建并与我的用户共享GoogleSheet。这些文档将使用特定的Google帐户创建,然后与我的用户共享

这就是我希望使用此服务器幻灯片流获取一次性授权代码并将其交换为刷新令牌的原因:

此刷新令牌将存储在我的数据库中,允许我代表此脱机用户使用Google服务

使用JavaScript库,我能够获得通过AJAX请求发送到服务器的一次性授权代码。

auth2.grantOfflineAccess({'redirect_uri': 'postmessage'}).then(grantOfflineAccessCallback);

var grantOfflineAccessCallback = function(authResult) {
    var auth_code = authResult.code;

    // Exchange the one-time authorization code for tokens
    $.post(...);
}
Client error: `POST https://www.googleapis.com/oauth2/v4/token` resulted in a `400 Bad Request` response:
{
 "error": "invalid_request",
 "error_description": "Missing parameter: redirect_uri"
}
在服务器端,我使用Google API PHP客户端(v2.0.0-RC6)获取访问和刷新令牌

$this->client = new Google_Client();
$this->client->setClientId($this->clientId);
$this->client->setClientSecret($this->clientSecret);
$this->client->setAccessType('offline');
$this->client->setApprovalPrompt('force');

$response = $this->client->fetchAccessTokenWithAuthCode($oneTimeCode);
我无法交换授权码。

auth2.grantOfflineAccess({'redirect_uri': 'postmessage'}).then(grantOfflineAccessCallback);

var grantOfflineAccessCallback = function(authResult) {
    var auth_code = authResult.code;

    // Exchange the one-time authorization code for tokens
    $.post(...);
}
Client error: `POST https://www.googleapis.com/oauth2/v4/token` resulted in a `400 Bad Request` response:
{
 "error": "invalid_request",
 "error_description": "Missing parameter: redirect_uri"
}
在这一页上,我们可以看到:

在服务器上,交换访问和刷新令牌的身份验证代码。 使用访问令牌代表用户调用Google API

关于JAVA示例代码:

REDIRECT_URI: // Specify the same redirect URI that you use with your web
              // app. If you don't have a web version of your app, you can
              // specify an empty string.
因为我处理的应用程序是intranet应用程序,所以在调用fetchAccessTokenWithAuthCode()方法之前,我尝试为此redirect_uri参数指定一个空字符串:

。。。
重定向URI中的结果必须是绝对的

我们可以在没有回调URL的情况下使用此混合服务器幻灯片流吗?

我的问题有什么解决办法吗

谢谢


编辑: 重定向uri是用户登录后将重定向到的位置。此URL必须在Google项目(开发者控制台)中注册所以重定向uri不是回调

现在通过以下方法解决了此问题:

$this->client->setRedirectUri('http://same.url.as.in.developers.console/');