options.method属性必须为字符串类型

时间:2020-08-19 20:41:07

标签: node.js express

我正在尝试使用Node.js和Express对SpotifyApi进行身份验证。服务器启动正常,这不是问题。但是我收到此错误:

SERVING ON PORT 8000
Now serving on 8080 at http://localhost:8080 
/home/aroe/projects/projectPerfect/spotifyApi.js:55
  if (error) throw new Error(error);
             ^

Error: TypeError [ERR_INVALID_ARG_TYPE]: The "options.method" property must be of type string. Received type function ([Function])
    at Request._callback (/home/aroe/projects/projectPerfect/spotifyApi.js:55:20)
    at self.callback (/home/aroe/projects/projectPerfect/node_modules/request/request.js:185:22)
    at Request.emit (events.js:310:20)
    at Request.start (/home/aroe/projects/projectPerfect/node_modules/request/request.js:753:10)
    at Request.end (/home/aroe/projects/projectPerfect/node_modules/request/request.js:1505:10)
    at end (/home/aroe/projects/projectPerfect/node_modules/request/request.js:564:14)
    at Immediate.<anonymous> (/home/aroe/projects/projectPerfect/node_modules/request/request.js:578:7)

这是我的代码:

const SpotifyWebApi = require('spotify-web-api-node');
const http = require("http");
const request = require('request');
const myserver = require('./server');
const querystring = require('querystring');
const { post } = require('request');
const express = require('express')
const https = require('follow-redirects').https;
        
//sets express server vars
const app = express()
const port = 8080

app.get('/', (req, res) => {
    res.send('Server Connected')
})
        
app.listen(port, () => {
    console.log(`Now serving on ${port} at ${process.env.URI} `)
})
        
require('dotenv').config();
        
//authenticate to SpotifyAPI
var spotifyApi = new SpotifyWebApi({
    clientId: process.env.CLIENT_ID,
    clientSecret: process.env.SECRET_ID,
    redirectUri: process.env.URI,
});
        
const scope = 'user-read-private user-read-email ';
const authUrl = 'https://accounts.spotify.com/api/token';
        
//Headers needed to auth to SpotifyAPI for Bearer token
const tokenHeaders = {
    'Authorization': process.env.ACCESSTOKEN,
    'Content-Type': 'application/x-www-form-urlencoded'
};
        
//spotify Auth Payload
const options = {
    'method': post,
    'url': authUrl,
    'headers': tokenHeaders
};
        
request(options,  (error, response) => {
    if (error) throw new Error(error);
    console.log(response.body);
});

在有问题的函数中,我试图将我的有效载荷发布回APIURL。我直接从Postman那里获得了这段代码的底部。

0 个答案:

没有答案