PayPal-Right Authentication-form

Hello everyone!

I have a problem to get my PayPal-REST-API to work.
My problem is about the PayPal-Authentication-process using Wix-Fetch .

Let’s take this example here, where you try to get ACCESS-TOKEN from PayPal…

export async function getAccessToken() {
 const response = await fetch("https://api.sandbox.paypal.com/v1/oauth2/token", {
        method: 'post',
        headers: {
             "Content-Type": "application/x-www-form-urlencoded",
             "Authorization": "Basic " + ""
        },
        body: "grant_type=client_credentials"
    });
 let jasonResponse = await response.json();
 return (jasonResponse);
}

The “Authorization”-part in the “headers”-section, is the problematic part.

I know what kind of type is expected and which parameters, but putting all the needed parameters directly into the CODE, does not work like it should.

The authorization-process asks for a STRING-combination of…
"Basic " + " + “:” +

Should be something like…

"Authorization": "Basic " + "Client-ID-Here" + ":" + "Secret-ID-here"

Regarding the CURL-CODING…


So, expected is a BASE64-encoded ClientID-Secret-combination separated by a —> “:”

'Authorization: Basic {Your Base64-encoded ClientID:Secret}'

Using POSTMAN, everything looks good and you get the ACCESS-TOKEN without any problems…

You get something like…

{
    "scope": "https://api.paypal.com/v1....,
    "access_token": "A21AAIESP7kstuC1o-5aIA8...",
    "token_type": "Bearer",
    "app_id": "APP-80W284485P...T",
    "expires_in": 32399,
    "nonce": "2022-09-12T15:05:08Z..."
}

…including the token-type and the token itself.

But when doing it directly without the usage of POSTMAN, you get a complete different response…

…something like…

Response:  {...}jsonWixCode_Console_Table_ViewWixCode_Console_Copy_JSON
url: "https://api.sandbox.paypal.com/v1/oauth2/token"
status: 200
statusText: "OK"
headers: {...}
ok: true
body: {...}
bodyUsed: false
size: 0
timeout: 0
_raw: [...]
_abort: false

Where you are able to find some buffered binary-data…

So still a step missing?

Hello! Sorry, no time to explain :slight_smile: But try not to use quotes around authorization. Not sure, but may help.

let auth = "Basic "+"";
...
headers:{
Authorization: auth,
"Content-Type":"application/x-www-form-urlencoded"
},

Sorry, no difference. Still the same.

You get “200” for status, so request seems to be ok.
Also the format of the BASE64-encoded ClientID-Secret-Combination
seems to be ok.

let authType = "Basic"
let encodedData = Buffer.from(`${clientID}:${secretID}`)
.toString('base64');
Authorization: `${authType} ${encodedData}`

Normaly you get a “400”-error if clientID and secret are not ok…
…something like…

EDIT:
Even using AXIOS-NPM works and you get your TOKEN!

But what am i missing in my own construction ?

Ahhh, sorry, when I read there was only the first part of your message.
Hmmmmmm🤔

The code a little bit more expanded…

export async function getAccessToken() {   
   let environment = "sandbox";
   let endpointURL = `https://api.${environment}.paypal.com/v1/oauth2/token`; 
   let authType = "Basic"
   let clientID = "Vc7YwRs_whatever......";                                                                  
   let secretID = "AFEx0_11tmsE_whatever....";                               
   let encodedData = Buffer.from(`${clientID}:${secretID}`).toString('base64');                                                        
     
   let options =  {
      method: 'post',
      headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            //"Authorization": authType+" "+encodedData       
            Authorization: `${authType} ${encodedData}`    
         },
         body: "grant_type=client_credentials" 
      }

   //----original-----------
   let response = await fetch(endpointURL, options);
   console.log("Response: ", response);

}

This one should normaly be the OUTPUT…

{
    "scope": "https://api.paypal.com/v1....,
    "access_token": "A21AAIESP7kstuC1o-5aIA8...",
    "token_type": "Bearer",
    "app_id": "APP-80W284485P...T",
    "expires_in": 32399,
    "nonce": "2022-09-12T15:05:08Z..."
}

Ok, i will reply my own QUESTION :laughing:, because i got it already to work (after few hours of intensive investigations, debugging and console-logging). :sweat_smile:

That was missing in my Code…

let response = await fetch(endpointURL, options);
let responseResult = await response.json(); console.log(responseResult);

Now it is possible to get TOKEN without any other 3-party codings, apps, npms, packages or whatever.