dtkmejg127475 2018-12-06 13:42
浏览 63

如何使用PHP从REST API获取令牌

I am trying to have access to an API that uses OAuth : https://developer.atpco.net/api/oauth/gettoken , and I'd like to be able to get my token in JSON form like this :

{
    "access_token": "CSX9w8dKQAThqUQYKalJT60ZXZ0fRe39lQu0GSs4ZC449G0F7tsaji",
    "token_type": "Bearer",
    "expires_in": 3600,
    "scope": "resource.WRITE resource.READ"
}

I have my client_id and my client_secret. Postman and Python work perfectly and I am able to retrieve the access-token, but I am having trouble replicating these results with PHP. Can you take a look at my code and provide any help please?

I have tried this solution :

<?php


session_start();

$CLIENT_ID = "xxxxxxxxxx";
$CLIENT_SECRET = "xxxxxxxxxxxxxxx";
$CALLBACK = "http://localhost";



function getAccessCode($client_id, $callback, $client_secret, $request_code) {
  $curl = curl_init("https://developer.atpco.net/api/oauth/gettoken");
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
  curl_setopt($curl, CURLOPT_HEADER, false);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_POSTFIELDS, rawurldecode(http_build_query(array(
    'client_id' => $client_id,
    'redirect_uri' => $callback,
    'client_secret' => $client_secret,
    'code' => $request_code,
    'grant_type' => 'authorization_code'
  ))));

  $json = json_decode(curl_exec($curl));
  return $json->access_token;
   sprintf($access_token);
}

but it doesn't seem to find my access-token :/

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思