Announcements

Help Wizard

Step 1

NEXT STEP

access token 415

access token 415

Plan

Free/Premium

Country

 

Device

(iPhone 8, Samsung Galaxy 9, Macbook Pro late 2016)

Operating System

(iOS 10, Android Oreo, Windows 10,etc.)

 

My Question or Issue

 

i try to get access token from a service in angular 17 application
the same code works if placed in app.component but if setted in a service return the 415 error

 

getToken(): void {
    const headers = new HttpHeaders({
      Authorization: 'Basic ' + btoa(`${this.clientId}:${this.clientSecret}`),
      'Content-Type': 'application/x-www-form-urlencoded',
    });

    const body = new URLSearchParams();
    body.set('grant_type', 'client_credentials');

    this.http
      .post<Token>(this.tokenUrl, body.toString(), { headers })
      .subscribe({
        next: (t: Token) => (data: any) => {
          this.token = t.access_token;
          this.token = data.access_token;
        },
        error: (error) => console.error('Error fetching access token:', error),
      });
  }

 

Reply
3 Replies

the code above has a mistake

please see this (with always 415 error)

getToken(): void {
    const headers = new HttpHeaders({
      Authorization: 'Basic ' + btoa(`${this.clientId}:${this.clientSecret}`),
      'Content-Type': 'application/x-www-form-urlencoded',
    });

    const body = new URLSearchParams();
    body.set('grant_type', 'client_credentials');

    this.http
      .post<Token>(this.tokenUrl, body.toString(), { headers })
      .subscribe({
        next: (t: Token) => {
          this.token = t.access_token;
        },
        error: (error) => console.error('Error fetching access token:', error),
      });
  }

I see that 'Quotation' marks are missing around Authorization.

XimzendSpotify Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.

I try that but with same errore

Tks for help

Suggested posts

Let's introduce ourselves!

Hey there you,   Yeah, you! 😁   Welcome - we're glad you joined the Spotify Community!   While you here, let's have a fun game and get…

ModeratorStaff / Moderator/ 4 years ago  in Social & Random