如何在PHP的Docusign API中修复curl错误“无法解析主机:account-d.docusign.com%2Foauth%2Ftoken”

时间:2019-10-10 09:59:16

标签: php curl docusignapi

我正在关注以下链接:
https://developers.docusign.com/esign-rest-api/guides/authentication/oauth2-code-grant

第1步工作正常,我可以收到授权码
步骤2无法正常工作会出现错误“无法解析主机:account-d.docusign.com%2Foauth%2Ftoken”

使用CURL在Apache服务器php 7.3上运行此程序。

$authcode = "";    
if(isset($_GET['code'])){    
    $authcode = $_GET['code'];    
}    

$headauth = base64_encode('bbc034da-a19f-48db-a841-25832e209a41:2f08bab1-2ccc-435f-a1f7-d43802b51255');    

$request_headers = array();    
$request_headers[] = 'Content-Type: application/x-www-form-urlencoded';    
$request_headers[] = 'Authorization: Basic '.$headauth;    

$ch = curl_init();    

$url = str_replace ( ' ', '%20', "https://account-d.docusign.com/oauth/token" );    

curl_setopt($ch, CURLOPT_URL,urlencode($url));    
curl_setopt($ch, CURLOPT_HEADER, 1);    
curl_setopt($ch, CURLOPT_POST, 1);    
curl_setopt($ch, CURLOPT_POSTFIELDS,"grant_type=authorization_code&code=".$authcode."&redirect_uri=http://192.168.2.56/testdocusign/test.php");  //Post Fields    
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);    
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  0);    
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);    

$server_output = curl_exec ($ch);    
if (curl_errno($ch)) {    
    $error_msg = curl_error($ch);    
    print_r($error_msg);    
}    

curl_close ($ch);    

print_r($server_output) ;    

我希望结果如下:

{    
    "access_token": "ISSUED_ACCESS_TOKEN",    
    "token_type": "Bearer",    
    "refresh_token": "ISSUED_REFRESH_TOKEN",    
    "expires_in": 28800    
}  

但是我当前的错误是:

  

无法解析主机:account-d.docusign.com%2Foauth%2Ftoken

1 个答案:

答案 0 :(得分:0)

您可以看到该网址不正确。您需要获取您说的第一次调用获得的访问代码就可以了,然后将其添加到第二个调用中,而不是%2Foauth%2Ftoken部分,该部分显然只是令牌的占位符。

(编辑:问题是网址编码不正确,这是罪魁祸首)