将javascript请求转换为API到CURL

时间:2018-06-14 00:06:03

标签: javascript php jquery ajax curl

我使用Javascript刷新Azure令牌,但现在它给了我一个错误:

No 'Access-Control-Allow-Origin' header is present on the requested resource.

所以我想改用CURL(PHP)。

这是Javascript:

var form_data = {
    "client_id": "**",
    "grant_type": "refresh_token",
    "resource": "https://analysis.windows.net/powerbi/api",
    "username": "****************",
    "password": "*****************",
    "scope": "openid",
    "client_secret": "**",
    "refresh_token": "**"
    };

    var options = {
    method: "post",
    headers: {
    "Content-Type": "application/x-www-form-urlencoded"
    },
    body: form_data
    }

    return fetch("https://login.microsoftonline.com/common/oauth2/token", options)
    .then((response) => {
    if (response.ok) {
    return response.json();
    } else {
    throw new Error("Server response wasn't OK");
    }
    })
    .then((json) => {
    return json.token;
    });

CURL:

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"https://login.microsoftonline.com/common/oauth2/token");
curl_setopt($ch, CURLOPT_POST, 1);
$client_id = "**";
$client_secret = "**";
curl_setopt($ch, CURLOPT_POSTFIELDS, );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);

完整的CURL请求应该如何?

0 个答案:

没有答案