使用php从BOX API获取访问令牌?

时间:2017-08-02 06:44:41

标签: php box

当我在终端上运行时,我试图从框API获取访问令牌,以下卷曲正在运行

curl https://api.box.com/oauth2/token \
-d  'grant_type=authorization_code&code=CODE&client_id=CLIENT_ID&client_secret=secret_ID' \
-X POST  # This is working.

上面的一个正在工作,但是我尝试过使用PHP的同样的事情,但是我试过以下代码会引发以下错误{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}1

$access_token_url = "https://api.box.com/oauth2/token";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $access_token_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'grant_type'=>'authorization_code', 
    'code'=>'code',
    'client_id'=>'id',
    'client_secret'=>'secret'
    ));
$response = curl_exec($ch);
curl_close($ch);

我不知道实际问题是什么。

3 个答案:

答案 0 :(得分:1)

您必须将数据设置为POST参数,而不是HEADER参数

$access_token_url = "https://api.box.com/oauth2/token";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $access_token_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
    'grant_type'=>'authorization_code', 
    'code'=>'code',
    'client_id'=>'id',
    'client_secret'=>'secret'
]));
$response = curl_exec($ch);
curl_close($ch);

答案 1 :(得分:1)

我认为这是因为命令行示例正在发出POST请求,但PHP curl请求不是。以下代码应该有助于您走上正确的轨道。

<?php
$access_token_url = "https://api.box.com/oauth2/token";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $access_token_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 
    array(
    'grant_type'=>'authorization_code', 
    'code'=>'code',
    'client_id'=>'id',
    'client_secret'=>'secret'));

$response = curl_exec($ch);
curl_close($ch);
?>

答案 2 :(得分:0)

您正在一起发送不同的参数。 -d option发送POST请求,因此您无法在GET中混合所有参数。按照提供的example

执行请求
FeatureSet fs = new FeatureSet(FeatureType.Polygon);

ProjectionInfo pStart = new ProjectionInfo();
pStart = KnownCoordinateSystems.Geographic.World.WGS1984;
fs.Projection = pStart;  
fs.DataTable.Columns.Add(new DataColumn("Sahə", typeof(string))); 

// create a geometry (square polygon)
List<Coordinate> vertices = new List<Coordinate>();

vertices.Add(new Coordinate(50.060444958508015, 40.55967497639358));
vertices.Add(new Coordinate(50.061042001470923, 40.554836019873619));
vertices.Add(new Coordinate(50.056969989091158, 40.554629992693663));
vertices.Add(new Coordinate(50.055287992581725, 40.559944035485387));
vertices.Add(new Coordinate(50.060444958508015, 40.55967497639358));

Polygon geom = new Polygon(vertices);

// add the geometry to the featureset. 
IFeature feature = fs.AddFeature(geom);

feature.DataRow.BeginEdit();
feature.DataRow["Sahə"] = "Əüei.."; 
feature.DataRow.EndEdit();


fs.SaveAs("C:\\test2.shp", true);