PHP Curl Basic OAuth获取访问令牌


PHP Curl Basic OAuth Getting Access Token

我试图从印度市场Flipkart提供的API获取访问令牌,如果我在Putty SSH中运行curl代码,我可以接收访问令牌,但如果我从PHP尝试同样的事情,它会给我400错误。

SSH(This Works)中使用的代码

curl -u appid:appsecret https://api.flipkart.net/oauth-service/oauth/token'?grant_type'=client_credentials'&scope=Seller_Api

我得到响应

{"access_token":"1111-xxxx-22222","token_type":"bearer","expires_in":4926731,"scope":"Seller_Api"}

但当我尝试使用PHP Curl(我只是在网上学习的)实现同样的事情时,我得到了400错误

使用的PHP代码(这不起作用)

<?php
$username='appid';
$password='appsecret';
$url='https://api.flipkart.net/oauth-service/oauth/token'?grant_type'=client_credentials'&scope=Seller_Api';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
$output = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
$info = curl_getinfo($ch);
curl_close($ch);
if(curl_errno($ch)){   
    echo 'Curl error: ' . curl_error($ch);
}
print_r($output);
echo $status_code;
?>

除了400(状态代码)之外,没有其他输出,这是一个错误的请求。

我知道我很接近,我不知道我做错了什么。如有任何帮助,我们将不胜感激。

附言:你可以在这里找到API文档https://seller.flipkart.com/api-docs/FMSAPI.html#third-第三方应用程序集成

我终于解决了这个问题,当我们尝试使用php-ccurl做同样的事情时,他们在文档中提到的url不起作用。

php 的Url短语不同

所以URL应该是$url='https://api.flipkart.net/oauth-service/oauth/token?grant_type=client_credentials&scope=Seller_Api';

瞧,我拿到了通行证。

附言:请不要认为我很不耐烦,在环顾四周之前过早地问了这个问题,这是我第二天遇到同样的问题了。但我今天很幸运;)