D
D
dmitriu2562019-04-01 15:07:10
JavaScript
dmitriu256, 2019-04-01 15:07:10

How to pass an authorization key in a get request?

I ask for help, how to pass the authorization key in the get request?
The key is valid as there is a result on the console spotify test page.
I make a service based on Spotify
Music search is carried out on such a request (from the documentation)


curl -X "GET" " https://api.spotify.com/v1/search?q=armin&type=tra... " -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer BQASa02dtw1lTM2wL-pslHAsf9ZRdMWFpfLmHmnTGXpmGWJzBN-_ALzbpweBl30sZvooeIvxgRRkYeJV7UY"

I'm trying to implement the following through the Service
File service
import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {HttpHeaders} from '@angular/common/http';

getMusic(music, type, limit, offset, key) {
    console.log(key.token);//ключ авторизации, можно задать и явно строкой, суть не меняется
    let headers = new HttpHeaders() 

   .append('Content-Type', 'application/json') // через метод set - результат такой же
   .append('Authorization', `Bearer ${key.token}`);
    
    return this.http.get(`https://api.spotify.com/v1/search?q=${music}&type=${type}&limit=${limit}&offset=${offset}`, {header: headers});

Error code
zone.js:3243 GET https://api.spotify.com/v1/search?q=armin&type=tra... 401
"status": 401,
"message": "Invalid access token"
Code of the component that gets the given method
this.spotify.getMusic(this.name, this.type, this.limit, this.offset, this.api).subscribe(data => console.log(data));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2019-04-01
@Sanasol

{header: headers}
https://angular.io/guide/http#adding-headers
headers: headers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question