Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

221
Visualizações
Paypal Rest Api returns 401 Unauthorized when i try to get OAuth2 token using php file_get_contents() in localhost with sandbox , but works in ajax

Paypal Rest Api returns 401 Unauthorized when i try to get OAuth2 token using php file_get_contents() in localhost with sandbox

    $client_id = env('PAYBAL_CLIENT');
    $client_secret = env('PAYPAL_SECRET');

    $opts = array('http' =>
        array(
            'method' => 'POST',
            "headers" => [
                "Content-Type" => "application/x-www-form-urlencoded",
                "Authorization" => "Basic " . base64_encode("$client_id:$client_secret")
            ],
            'body' => "{'grant_type' : 'client_credentials'}"
        )
    );

    $context = stream_context_create($opts);

    $url = "https://api-m.sandbox.paypal.com/v1/oauth2/token";

    $result = file_get_contents($url, false, $context);

the same requeste worked fine with me in ajax

$.ajax(
{
    url: "https://api-m.sandbox.paypal.com/v1/oauth2/token",
    type:"post",
    headers:{
        "Authorization": "Basic 'XXXbase64_encodedXXX'"
        "Content-Type": "application/x-www-form-urlencoded"
    },
    data:{
        "grant_type":"client_credentials"
    },
    success:function (data){
        console.log(data);
    },
    complete:function (data,status){
        console.log(status);
    }
}

);

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

The ajax function is converting the data to url encoded format for you here:

        "Content-Type": "application/x-www-form-urlencoded"
    },
    data:{
        "grant_type":"client_credentials"
    },

PHP's curl does not do that (or especially when you give it a string instead of an array/object, there's no way it can guess that it needs to be converted; it will assume any string you pass is the string you intend to post)

'body' => "{'grant_type' : 'client_credentials'}"

You must supply a form url-encoded body string as the API call requires: "grant_type=client_credentials"

about 4 years ago · Santiago Gelvez Relatório

0

Instead of file_get_contents I uses GuzzleHttp And It Worked With Me , Here is the code

        $client_id = env('PAYBAL_CLIENT');
    $client_secret = env('PAYPAL_SECRET');
    
    $client = new \GuzzleHttp\Client();
    $response = $client->request('POST', 'https://api-m.sandbox.paypal.com/v1/oauth2/token', [
        'headers' => [
            'Content-type' => 'application/x-www-form-urlencoded',
            'Authorization' => "Basic " . base64_encode("$client_id:$client_secret")
        ],
        'body' => 'grant_type=client_credentials'
    ]);
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda