如何使用Google一次性授权代码交换刷新令牌而无需回调(内部网)?

Gui*_*ara 5 google-api google-oauth google-api-php-client google-oauth2

我正在使用基于Intranet的应用程序,并且想使用Google服务。目前,我已经使用JavaScript客户端身份验证成功实现了带有“网站登录”功能的Google身份验证。我的用户现在可以登录或注册其Google帐户。

现在,我想使用Google API创建我的用户并与他们共享Google表格。这些文档将使用特定的Google帐户创建,然后与我的用户共享。

这就是为什么我要使用此服务器幻灯片流程获取一次性授权代码并将其交换为刷新令牌的原因:https : //developers.google.com/identity/sign-in/web/server-side-流

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(...);
}
Run Code Online (Sandbox Code Playgroud)

在服务器端,我使用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);
Run Code Online (Sandbox Code Playgroud)

我无法交换授权码。

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"
}
Run Code Online (Sandbox Code Playgroud)

在此页面上,我们可以阅读:

在服务器上,将身份验证代码交换为访问和刷新令牌。使用访问令牌代表用户调用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.
Run Code Online (Sandbox Code Playgroud)

因为我正在处理的应用程序是Intranet应用程序,所以在调用fetchAccessTokenWithAuthCode()方法之前,我尝试为此redirect_uri参数指定一个空字符串:

$this->client->setRedirectUri('');
Run Code Online (Sandbox Code Playgroud)

...导致Redirect URI must be absolute

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

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

谢谢,


编辑:

redirect_uri是用户登录后将被重定向到的位置。此URL必须在Google Project(开发者控制台)中注册。所以redirect_uri不是回调...!

现在可以通过以下方式解决问题:

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