PayPal REST令牌请求:api.paypal.com正常,API-m失败 [英] PayPal REST token request: api.paypal.com OK, api-m fails

查看:13
本文介绍了PayPal REST令牌请求:api.paypal.com正常,API-m失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

附录2:Mozilla行为特定于URL解析到的主机;我已经在问题的末尾添加了显示这一点的cURL脚本。

附录:这个在8个小时后就消失了,并且工作了好几天。但一周后,我重新运行该页面,只是为了检查,它再次失败:api.paypal起作用,api-m.paypal不起作用。

我收到来自api-m.paypal.com和api.paypal.com的请求实时站点访问令牌的不同结果。如果我向api.paypal.com发出请求,它会正常工作并返回一个令牌。如果我从api-m.paypal.com请求它,我会收到403禁止错误。这怎麽可能?一般来说,对于令牌请求,文档似乎交替使用API和API-m。这两者之间有什么不同?哪些调用应该被路由到API而不是API-m?当我在沙箱上运行我的整个商店时,所有都转到api-m,并且运行得很好。在一个只重复请求令牌的测试程序中,通过api、api-m、api.Sandbox和api-m.Sandbox进行排序-只有API-m失败,其他3种情况都是好的。我看过一次API与API-m的讨论,但再也找不到了;我很确定它没有提到这一点!

<?php

include("../_private/ppinfo.php");

header('Content-type: text/plain');
$sandbox = 0;
echo "sandbox $sandbox rv " . GetNewPPToken($sandbox) . "
";
$sandbox = 1;
echo "sandbox $sandbox rv " . GetNewPPToken($sandbox) . "
";
$sandbox = 2;
echo "sandbox $sandbox rv " . GetNewPPToken($sandbox) . "
";
$sandbox = -1;
echo "sandbox $sandbox rv " . GetNewPPToken($sandbox) . "
";
$sandbox = 0;
echo "sandbox $sandbox rv " . GetNewPPToken($sandbox) . "
";
$sandbox = -1;
echo "sandbox $sandbox rv " . GetNewPPToken($sandbox) . "
";
$sandbox = 1;
echo "sandbox $sandbox rv " . GetNewPPToken($sandbox) . "
";
$sandbox = 0;
echo "sandbox $sandbox rv " . GetNewPPToken($sandbox) . "
";
$sandbox = 2;
echo "sandbox $sandbox rv " . GetNewPPToken($sandbox) . "
";

// Get a paypal REST token to use for the rest of our transactions.
// See https://developer.paypal.com/docs/business/get-started/

function GetNewPPToken($sandbox) 
{
    global $G, $ppinfo;

    $headers = array(
            "Accept: application/json",
            "Accept-Language: en_US",
            "Content-Type: application/x-www-form-urlencoded"
        );
    if ($sandbox > 1)
    {
    $clid = $ppinfo['sb_acct'];
    $secret = $ppinfo['sb_secr'];
    $url = "https://api.sandbox.paypal.com/v1/oauth2/token";
    }
    else if ($sandbox > 0)
    {
    $clid = $ppinfo['sb_acct'];
    $secret = $ppinfo['sb_secr'];
    $url = "https://api-m.sandbox.paypal.com/v1/oauth2/token";
    }
    else if ($sandbox < 0)
    {
    $clid = $ppinfo['acct'];
    $secret = $ppinfo['secr'];
    $url = "https://api.paypal.com/v1/oauth2/token";
    }
    else
    {
    $clid = $ppinfo['acct'];
    $secret = $ppinfo['secr'];
    $url = "https://api-m.paypal.com/v1/oauth2/token";
    };

    $cvt = "grant_type=client_credentials";
    $curl = newPPcurl($url, $cvt, $headers);
    curl_setopt($curl, CURLOPT_USERPWD, "$clid:$secret");
    $resp = curl_exec($curl);
    $err = curl_error($curl) ;
    $json = json_decode($resp, true);

    if (0)
    {
    echo "response:
";
    print_r($resp);
    echo "err:
";
    print_r($err);
    echo "token '" . $ppinfo['token'] . "'
";
    };

    $ppinfo['token'] = $json['access_token'];
    return ($ppinfo['token'] != '' ? 1 : 0);
}



function newPPcurl($url, $flds, $hdrs)
{
    $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, 1);
    if ($flds != '')
    curl_setopt($curl, CURLOPT_POSTFIELDS, $flds);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
    curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,  2); // or 2?
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE);
    //curl_setopt($curl, CURLOPT_SSLCERT, $pem);  // pem file name
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
    if ($hdrs != "")
    curl_setopt($curl, CURLOPT_HTTPHEADER, $hdrs);

    return $curl;
}

测试输出:

sandbox 0 rv 0
sandbox 1 rv 1
sandbox 2 rv 1
sandbox -1 rv 1
sandbox 0 rv 0
sandbox -1 rv 1
sandbox 1 rv 1
sandbox 0 rv 0
sandbox 2 rv 1

下面是一些显示此行为的cURL命令。当api-m.paypal.com解析为184.87.90.6时,可以使用Mozilla代理(cmd#1)获取令牌。当IP解析为151.101.1.35时,对Mozilla(cmd#2)的请求失败,传递为cURL(cmd#3)。请注意,您必须提供您自己的id:pwd字符串以进行测试。

curl -v https://api-m.paypal.com/v1/oauth2/token 
    --user-agent "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" 
    --resolve api-m.paypal.com:443:184.87.90.6 
  -H "Accept: application/json" 
  -H "Accept-Language: en_US" 
  -u "<id:pwd>" 
  -d "grant_type=client_credentials"
curl -v https://api-m.paypal.com/v1/oauth2/token 
    --user-agent "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" 
    --resolve api-m.paypal.com:443:151.101.1.35 
  -H "Accept: application/json" 
  -H "Accept-Language: en_US" 
  -u "<id:pwd>" 
  -d "grant_type=client_credentials"
curl -v https://api-m.paypal.com/v1/oauth2/token 
    --user-agent "curl/7.55.1" 
    --resolve api-m.paypal.com:443:151.101.1.35 
  -H "Accept: application/json" 
  -H "Accept-Language: en_US" 
  -u "<id:pwd>" 
  -d "grant_type=client_credentials"

推荐答案

PayPal REST令牌请求的用户代理必须是cURL标识符,例如cURL/7.55.1&。使用mozilla用户代理会导致api-m.paypal.com上的403被禁用,但它似乎可以在api.paypal.com、api.Sandbox.paypal.com和api-m.Sandbox.paypal.com上运行。

这篇关于PayPal REST令牌请求:api.paypal.com正常,API-m失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆