David Trespalacios
PreguntaHola, estoy implementando paypal para mi app, pero me topo con comandos curl y no sé como usarlos con el httpClient. si alguien tiene info se lo agradezco
Codigo a “traducir” de curl a httpClient
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \ -H "Accept: application/json" \ -H "Accept-Language: en_US" \ -u "client_id:secret" \ -d "grant_type=client_credentials"
Nicolás Arias González
Las opciones que están con -H son headers, debes usar
HttpHeaders{ Accept: 'application/json', 'Accept-Language': 'en_us'}La opción -u es la información de autenticación, la puedes agregar en la url como
https://<client_id>:<secret>@api.sandbox.paypal.comAuthorization: Basic YWxhZGRpbjpvcGVuc2VzYW1lFinalmente, la opción -d es la forma de CURL de enviar información en el body, así que lo que ves ahí debe ir en el segundo parámetro de tu llamada a
HttpClient.post{ 'grant_type': 'client_credentials'}David Trespalacios
Gracias por la ayuda, sigo sin poder lograrlo 😢. Este es mi codigo en el servicio:
import { Injectable } from "@angular/core"; import { HttpClient } from "@angular/common/http"; import { HttpHeaders } from "@angular/common/http"; import { Observable } from "rxjs"; @Injectable({ providedIn: "root", }) export class PaypalService { constructor(private http: HttpClient) {} public getToken(): Observable<any> { const idCliente = "AVTSdxiyGkG6ChhS1BGEbhIe-2g60naoGoX_2xibb0Igj-vtDBbmbLn9TjAdJkPeZ3pYklOQ2XbB20H5"; const secret = "EEzPuVeqs_TH_J3JuzBnnViIB87p6UcuOlZOpUeeTu85tnYSyrVa-K75OlAJHDhzSO4BWMrgJ2grx4eX"; const url = `https://api.sandbox.paypal.com/v1/oauth2/token`; const credencial = btoa(`${idCliente}:${secret}`); const options = { headers: new HttpHeaders({ Accept: "application/json", "Accept-Language": "en_US", Authorization: `Basic ${credencial}`, grant_type: "client_credentials", }), }; return this.http.post(url, options); } }
Pero me dice en la consola que no estoy autorizado, pero hago la prueba con el comando Curl y Postman y ambos funcionan con las mismas credenciales.
HttpErrorResponse {headers: HttpHeaders, status: 401, statusText: "Unauthorized", url: "https://api.sandbox.paypal.com/v1/oauth2/token", ok: false, …} error: {name: "AUTHENTICATION_FAILURE", message: "Authentication failed due to invalid authentication credentials or a missing Authorization header.", links: Array(1)} headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ} message: "Http failure response for https://api.sandbox.paypal.com/v1/oauth2/token: 401 Unauthorized" name: "HttpErrorResponse" ok: false status: 401 statusText: "Unauthorized" url: "https://api.sandbox.paypal.com/v1/oauth2/token" __proto__: HttpResponseBase
Comando curl usando con respuesta positiva:
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \ -H "Accept: application/json" \ -H "Accept-Language: en_US" \ -u "AVTSdxiyGkG6ChhS1BGEbhIe-2g60naoGoX_2xibb0Igj-vtDBbmbLn9TjAdJkPeZ3pYklOQ2XbB20H5:EEzPuVeqs_TH_J3JuzBnnViIB87p6UcuOlZOpUeeTu85tnYSyrVa-K75OlAJHDhzSO4BWMrgJ2grx4eX" \ -d "grant_type=client_credentials"
David Trespalacios
Después de varias horas... el tema del autoaprendizaje :). Cambiando la linea de lugar:
grant_type: "client_credentials",
e integrandola como body de la llamada POST me ha funcionado:
return this.http.post(url, "grant_type=client_credentials", options);
Espero que a otro notavo le sirva. jejeje
![Curso de Angular [Empieza Gratis]](https://static.platzi.com/media/courses/Opengraph-angular.png)