What is the Placement of Grant_Type in XHR?

I attempted to access my OAuth2 Token through an XHR-Request in TypeScript, but I consistently receive a 400 error with the message: {"error":"unsupported_grant_type","error_description":"Grant Type is NULL"}

The request is being made from a NativeScript Application (using a TypeScript Template) running on an iOS emulator.

Here is my code:

    var oReq = new XMLHttpRequest();     
    oReq.open("POST", "https://api.sandbox.paypal.com/v1/oauth2/token", true);
  //  oReq.withCredentials = true;
  //  oReq.setRequestHeader('grant_type', "client_credentials");
  // where client_credentials = clientID:secret 

    oReq.setRequestHeader('Authorization', "Basic " + this.authorizationBasic); 
//where AutorizationBasic is the base64 String of my credentials
    oReq.setRequestHeader('Accept', "application/json");
    oReq.setRequestHeader('Accept-Language', "en_US");

    oReq.onreadystatechange = function () {
        console.log("state changed - new state: " + oReq.readyState + " and Status: " + oReq.status);
        if (oReq.readyState === 4) {
            if (oReq.status === 200) {
                console.log(oReq.responseText);
            } else { 
                console.log("Error: " + oReq.status + " Message: " + oReq.responseText);
            } 
        }    
    };
    console.log("Status: " + oReq.status);  
    oReq.send();

Upon researching, it appears that others have faced similar issues with the PayPal API, as discussed here. Has anyone managed to resolve this problem? Could there be an issue with my code? I have tried various approaches, as seen in the commented lines above, without success.

Check out the PayPal API documentation here: https://developer.paypal.com/docs/integration/direct/make-your-first-call/

I am growing increasingly frustrated as this simple API call is taking longer than expected. The curl command with my clientID and secret worked for me. Does anyone have any suggestions on how to proceed? Is there a more straightforward method than using XHR?

Answer №1

If you know someone who is looking for this information:

Make sure to include the Grant Type in the xhr.send() method body:

xhr.send('grant_type=client_credentials');

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What settings do I need to adjust in order to set up VSCode to be compatible with TypeScript

I'm currently struggling to set up VSCode with TypeScript and facing some difficulties. My focus is on the following: https://code.visualstudio.com/docs/languages/typescript It seems like after installing the compiler, VSCode should function proper ...

The function service.foo is not recognized in Angular

My service function is not being recognized by my component import { Injectable } from '@angular/core'; import { ToastController } from '@ionic/angular'; @Injectable({ providedIn: 'root' }) export class LocationService { ...

Tips for creating an array:

Currently, I am working on a project that involves using an array of objects. While I have a good understanding of what an array is and how to create an object, I am struggling with incorporating my student object into the array. In this project, each st ...

Updating views in Angular 2 based on changes in component properties

I recently set up a WebSocket service and component, but I'm facing challenges with updating the view when new data is received through the WebSocket connection. websocket.service.ts import {Injectable} from "angular2/core"; import {Observable} from ...

Is there a way to increase the row size only when it is clicked on?

I am facing an issue with my material-ui table. Whenever I click the button to expand row1, it also expands the second row simultaneously. How can I resolve this so that only the specific row I click on expands? <Table stickyHeader aria-label="st ...

Obtaining the data stored in objects within a parse database

I'm currently facing an issue where I am trying to retrieve the name of the creator from the session object, which is a pointer. For testing purposes, I have been attempting to access this information but it keeps showing up as undefined. Any suggesti ...

A Comprehensive Guide to Fetching Superset Dashboard Thumbnail Image in an Angular Application Through API Integration

Greetings to the wonderful Stack Overflow community! I have been working diligently on incorporating Superset into my Angular application, but I have encountered a hurdle when attempting to retrieve the thumbnail image of a specific Superset dashboard. Cu ...

Guide on how to display matching options in an input box using HTML datalist based on user input at the beginning

I am a beginner in React and I am looking to create an autocomplete suggestion box for a text input field. I want the suggestions to only match the first letters of the available options, unlike the current behavior of HTML's <datalist>. Althou ...

Filter Angular by columns that change dynamically

I have a filter function that works well when I use a static column name like this: this.listOfData = this.listOfData.filter((item: DataItem) => item.name.toLowerCase().indexOf(newValue.toLowerCase()) !== -1 ); Note: item.name However, I need to sea ...

Combining two collections in MongoDB and calculating the total number of documents

I'm seeking guidance on working with mongodb and node.js. I currently have a collection called PIZZA { title: "bacon" } { title: "pepperoni" } as well as another collection named ORDERS { orderid: 1, pizza: "bacon" } { orderid: 2, pizza: "pepperoni ...

What is the best way to incorporate multiple variables in a MySQL query when using Node.js?

I'm facing a challenge where I need to input student data into a table using the parent key as a foreign key. The parent information is included in the same JSON object, with an array of students inside it. My goal is to retrieve the parent Id from th ...

What is the best way to determine if a mat-menu in Material Angular is currently displaying or hidden?

Is there a way to determine if the mat-menu is currently open so that I can dynamically apply a class to the corresponding button using [ngClass] depending on the menu's state? <button mat-stroked-button mdbWavesEffect [matMenuTriggerFor]="menu"&g ...

Combining two streams in RxJS and terminating the merged stream when a particular input is triggered

I am currently developing an Angular application and working on implementing a system where an NGRX effect will make requests to a service. This service will essentially perform two tasks: Firstly, it will check the local cache (sqlite) for the requested ...

Is it possible for an AJAX call to fail, despite receiving a valid 200OK response?

There are times when the server responses return a 200OK status, but the content of the response is not what was anticipated. Is it possible to invoke the jquery ajax error callback even with a successful response, depending on certain criteria within the ...

Utilizing Node.js within a closed intranet environment

Utilizing Nodejs's npm has proven to be quite convenient. Thus, I made the decision to incorporate it into my company's project. However, a predicament arises as my company mandates development within a closed network. This restricts my access s ...

What is the best way to implement pipes and incorporate reusable action buttons in a Mat-table component for maximum reusability?

I am seeking assistance in creating a reusable component for the Angular Material Mat-table. I have made progress on loading data from the parent component to the child component, as can be seen in StackBlitz, but I now want to apply pipes to the data bef ...

Having trouble utilizing the $ selector within a for loop? Avoid attempting to use $("$i") and try an alternative method instead

for (var i = 0; i < 9; i++) { a[i] = val($("#" + i)); alert(a[i]); } UPDATE: The previous code was incorrect as it selected elements with id(#) 'i', which is invalid. Therefore, "#" + i should be used instead. My query was: How c ...

Using webpack to access a bundled React class in an HTML file

Is there a way to access pre-built components directly within the HTML file that links to the build? I've been attempting the following - Inside bundle.js var React = require('react'); var ReactDOM = require('react-dom'); ...

Updating div constantly with onclick using jQuery Ajax

I've been attempting to refresh the content of a div after clicking a button, but it only happens once. I have to refresh the page in order to click the button again and update the content. Since I'm new to AJAX, I need some help resolving this i ...

What is the best way to fill a JavaScript array with only the divs that are currently displaying as block elements?

In my JavaScript code, I am attempting to populate an array using jQuery. Within a <section> element, there are multiple <div> elements, but I only want to add the ones that are visible (with CSS property display: block) to the array. HTML: & ...