在 Dropbox 中使用“使用 Google 登录”时交换访问令牌代码失败
Exchanging code for access token fails when using Sign in with Google in Dropbox
我们有一个使用 Dropbox 的应用程序 API。当用户通过 Dropbox OAuth 2 流程并使用他们的电子邮件地址和密码登录时,一切正常,我们得到 access_token
。但是,当用户在 Dropbox 授权对话框中使用 "Sign in with Google" 流程时,我们返回 code
然后我们尝试交换访问令牌,但请求失败 {"error_description": "code doesn't exist or has expired", "error": "invalid_grant"}
.
这是我们使用的步骤:
1.
var dbx = new Dropbox.Dropbox({ clientId: clientId });
var authUrl = dbx.getAuthenticationUrl('https://www.dropbox.com/1/oauth2/redirect_receiver');
这给了我们 url https://www.dropbox.com/oauth2/authorize?response_type=token&client_id=...&redirect_uri=https://www.dropbox.com/1/oauth2/redirect_receiver
.
2。
在弹出窗口中打开 authUrl
。
3。
用户使用 "Sign in with Google"
4。
我们将重定向到下面包含代码的 URL:
https://www.dropbox.com/google/authcallback?state=...&code=...&scope=...
现在尝试将 POST 的访问令牌代码交换为 https://api.dropboxapi.com/oauth2/token
给我们:
{"error_description": "code doesn't exist or has expired", "error": "invalid_grant"}
这里的问题是,如果使用 Google 登录流程,实际上会出现两个 OAuth 授权流程实例; Google 登录流程嵌套在 Dropbox 应用程序授权流程中。不过,您的应用实际上并不需要知道这一点。
https://www.dropbox.com/google/authcallback
URL 是 Dropbox 的重定向 URL Google 登录流程,相应地 code
是 Google OAuth 流程,不是 Dropbox OAuth 流程。如您所见,尝试将其用于 Dropbox OAuth 2 流程将相应地失败(因为它实际上来自 Google,而不是 Dropbox)。
你应该让你的应用等到你自己的重定向 URL(在你的共享代码中,https://www.dropbox.com/1/oauth2/redirect_receiver
)被访问,然后才从那里获取 code
并交换它用于 Dropbox 访问令牌。
我们有一个使用 Dropbox 的应用程序 API。当用户通过 Dropbox OAuth 2 流程并使用他们的电子邮件地址和密码登录时,一切正常,我们得到 access_token
。但是,当用户在 Dropbox 授权对话框中使用 "Sign in with Google" 流程时,我们返回 code
然后我们尝试交换访问令牌,但请求失败 {"error_description": "code doesn't exist or has expired", "error": "invalid_grant"}
.
这是我们使用的步骤:
1.
var dbx = new Dropbox.Dropbox({ clientId: clientId });
var authUrl = dbx.getAuthenticationUrl('https://www.dropbox.com/1/oauth2/redirect_receiver');
这给了我们 url https://www.dropbox.com/oauth2/authorize?response_type=token&client_id=...&redirect_uri=https://www.dropbox.com/1/oauth2/redirect_receiver
.
2。
在弹出窗口中打开 authUrl
。
3。 用户使用 "Sign in with Google"
4。
我们将重定向到下面包含代码的 URL:
https://www.dropbox.com/google/authcallback?state=...&code=...&scope=...
现在尝试将 POST 的访问令牌代码交换为 https://api.dropboxapi.com/oauth2/token
给我们:
{"error_description": "code doesn't exist or has expired", "error": "invalid_grant"}
这里的问题是,如果使用 Google 登录流程,实际上会出现两个 OAuth 授权流程实例; Google 登录流程嵌套在 Dropbox 应用程序授权流程中。不过,您的应用实际上并不需要知道这一点。
https://www.dropbox.com/google/authcallback
URL 是 Dropbox 的重定向 URL Google 登录流程,相应地 code
是 Google OAuth 流程,不是 Dropbox OAuth 流程。如您所见,尝试将其用于 Dropbox OAuth 2 流程将相应地失败(因为它实际上来自 Google,而不是 Dropbox)。
你应该让你的应用等到你自己的重定向 URL(在你的共享代码中,https://www.dropbox.com/1/oauth2/redirect_receiver
)被访问,然后才从那里获取 code
并交换它用于 Dropbox 访问令牌。