在Dropbox中使用“通过Google登录”时,将代码交换为访问令牌失败

时间:2018-09-04 16:17:54

标签: dropbox dropbox-api

我们有一个使用Dropbox API的应用程序。当用户浏览Dropbox OAuth 2流程并使用其电子邮件地址和密码登录时,一切正常,我们得到了access_token。但是,当用户在Dropbox授权对话框中使用“使用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');

这为我们提供了网址https://www.dropbox.com/oauth2/authorize?response_type=token&client_id=...&redirect_uri=https://www.dropbox.com/1/oauth2/redirect_receiver

2。  在弹出窗口中打开authUrl

3。  用户使用“使用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"}

1 个答案:

答案 0 :(得分:1)

这里的问题是,考虑到使用了Google登录流程,实际上存在两个OAuth授权流程实例; Google登录流程嵌套在Dropbox应用授权流程中。您的应用实际上实际上不需要了解这一点。

https://www.dropbox.com/google/authcallback URL是Dropbox的Google登录流重定向URL,因此,给定的code是Google OAuth流,而不是Dropbox OAuth流。如您所见,尝试将其用于Dropbox OAuth 2流将会失败(因为它实际上来自Google,而不是Dropbox)。

您应该让您的应用等待,直到访问您自己的重定向URL(在共享代码中为https://www.dropbox.com/1/oauth2/redirect_receiver),然后再从那里获取code并将其交换为Dropbox访问令牌。