php curl无法与Dropbox Oauth一起工作,返回“无效请求”

发布于 2025-01-20 14:48:34 字数 1224 浏览 3 评论 0原文

Dropbox文档说要进行身份验证您必须执行以下操作:

curl https://api.dropbox.com/oauth2/token \
    -d code=<AUTHORIZATION_CODE> \
    -d grant_type=authorization_code \
    -d redirect_uri=<REDIRECT_URI> \
    -u <APP_KEY>:<APP_SECRET>

这是我对PHP的尝试,但是在错误之后返回dropbox 无效请求 - 请求参数与任何支持的授权流不匹配

$url = "https://api.dropbox.com/oauth2/token";
            $redirect_uri = 'https://www.mywebsite.com/oauth_dropbox';
           
            $postfields = array(
                'code' => $_GET['code'],
                'grant_type' => 'authorization_code',
                'redirect_uri' => $redirect_uri
            );

            $auth = "$app_key:$app_secret";
            $curl = curl_init($url);

            curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postfields));
            curl_setopt($curl, CURLOPT_USERPWD, $auth);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

            $json_response = curl_exec($curl);
            $responseObj = json_decode($json_response);

我如何求解?

Dropbox documentation says to authenticate you have to do the following:

curl https://api.dropbox.com/oauth2/token \
    -d code=<AUTHORIZATION_CODE> \
    -d grant_type=authorization_code \
    -d redirect_uri=<REDIRECT_URI> \
    -u <APP_KEY>:<APP_SECRET>

Here is my attempt with PHP but dropbox returns following error invalid request - The request parameters do not match any of the supported authorization flows

$url = "https://api.dropbox.com/oauth2/token";
            $redirect_uri = 'https://www.mywebsite.com/oauth_dropbox';
           
            $postfields = array(
                'code' => $_GET['code'],
                'grant_type' => 'authorization_code',
                'redirect_uri' => $redirect_uri
            );

            $auth = "$app_key:$app_secret";
            $curl = curl_init($url);

            curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postfields));
            curl_setopt($curl, CURLOPT_USERPWD, $auth);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

            $json_response = curl_exec($curl);
            $responseObj = json_decode($json_response);

How do i solve?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弱骨蛰伏 2025-01-27 14:48:34

如果您明确设置基本授权,那就可以做到这一点,也就是说:

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

设置以下操作:

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

It looks like this works if you explicitly set Basic authorization, that is, instead of:

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

set this:

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
雨夜星沙 2025-01-27 14:48:34

您当前的curl发送GET请求(这就是外部API看不到发送的任何参数的原因),但您正在添加内容,因为它是POST请求。也添加CURLOPT_POST

$url = "https://api.dropbox.com/oauth2/token";
$redirect_uri = 'https://www.mywebsite.com/oauth_dropbox';
           
$postfields = [
    'code' => $_GET['code'],
    'grant_type' => 'authorization_code',
    'redirect_uri' => $redirect_uri
];

$auth = "$app_key:$app_secret";
$curl = curl_init($url);

curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postfields));
curl_setopt($curl, CURLOPT_USERPWD, $auth);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$json_response = curl_exec($curl);
$responseObj = json_decode($json_response);

Your current curl sends GET request (that's why external API does not see any parameters sent), but you are adding content as it's POST request. Add CURLOPT_POST too.

$url = "https://api.dropbox.com/oauth2/token";
$redirect_uri = 'https://www.mywebsite.com/oauth_dropbox';
           
$postfields = [
    'code' => $_GET['code'],
    'grant_type' => 'authorization_code',
    'redirect_uri' => $redirect_uri
];

$auth = "$app_key:$app_secret";
$curl = curl_init($url);

curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postfields));
curl_setopt($curl, CURLOPT_USERPWD, $auth);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$json_response = curl_exec($curl);
$responseObj = json_decode($json_response);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文