【问题标题】:I am not able to get a proper response through browser, but through command line php works perfectly我无法通过浏览器获得正确的响应,但通过命令行 php 可以完美运行
【发布时间】:2014-03-03 21:33:54
【问题描述】:

现在通过命令行运行,我得到了预期的 JSON 响应和成功的身份验证。 但是通过浏览器,我得到了错误。 编辑我能够进行身份验证并获取代码。如果我输入代码而不是变量,我可以从命令行获得响应。

也已经过测试并且能够为实际字符串分出变量,并且仍然通过命令行而不是浏览器进行身份验证。

function tokenRequest($clientId, $clientSecret, $code){

$url =  'https://www.strava.com/oauth/token?';
$oauthFields = array(
    'client_id' => $clientId,
    'client_secret' => $clientSecret,
    'code' => $code);

$parameters = '&' . http_build_query($oauthFields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.$parameters);
curl_setopt($ch, CURLOPT_POST, count($oauthFieldsObject));
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
$urlResponse = curl_exec($ch);


curl_close($ch);
return (json_decode($urlResponse));
}

预期是 json 响应,但在我的主页上我对其进行了解码,但没有显示任何内容。下面是尝试显示 json 数据的代码。

$token = tokenRequest($client_id, $client_secret, $codeUrl);
echo $token;
var_dump($token);

【问题讨论】:

    标签: php json curl oauth-2.0


    【解决方案1】:
    1. 您必须查看他们使用的 oauth 版本
    2. 您的功能不够,您必须先请求 requestToken,然后再请求 accessToken,然后您必须请求用户数据才能正常工作

    看这里:http://strava.github.io/api/v3/oauth/

    【讨论】:

    • 我可以通过命令行获取json数据,请看上面的编辑。我已经倾注了他们的 API oauth 页面。 (记住)
    • ok 然后用 $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); http状态码
    • 添加返回响应:0
    • sry 使用这个pastebin.com/r0JEAsD8 我讨厌这个函数的类型我得到这个返回对象(stdClass)#1 (2) { ["message"]=> string(19) "Authorization Error" ["errors"]=> array(1) { [0]=> object(stdClass)#2 (3) { ["resource"]=> string(11) "Application" ["field"]=> string( 0) "" ["code"]=> 字符串(7) "无效" } } }
    • 是的,如果没有所需的数据,它会失败,因为我正在尝试授权。如果填写了这些值,我会得到 'code'object(stdClass)#2 (2) { ["access_token"]=> string(40) "**************** ******" ["运动员"]=> 对象(stdClass)#3 (25) { ["id"]=> int(165762) ["resource_state"]=> int(3) ["firstname" ]=> 字符串(3) "***" ["姓氏"]=>
    【解决方案2】:

    我想通了,我补充了:

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
    

    因为它是一个 https 网站。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多