Where do i put the user token for api request

89 views Asked by At

I have this sample code to get the users albums, but where do I put the usertoken on the request.get call. I can't find anywhere online that shows where it goes.

"use strict";

const fs = require("fs");

const jwt = require("jsonwebtoken");

const request = require("request");

const privateKey = fs.readFileSync("AuthKey.p8").toString();

const jwtToken = jwt.sign({}, privateKey, { algorithm: "ES256", expiresIn: "180d", issuer: "", header: { alg: "ES256", kid: "" } });

console.log("token:", jwtToken, "\n");

var url = "";

url = "https://api.music.apple.com/v1/me/library/albums";

request.get( 
{ url: url, auth: { bearer: jwtToken }, json: true }, (err, httpResponse, body) => { if (err) { console.error(err); } else { console.log(body.results.albums.data); } 
} );
1

There are 1 answers

1
Meysam Najafi On

You can simply attach the token to the request with query parameter or in the Authorization header. Putting it in the header is a better way. You can read that for more info: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization