stuchlyf
stuchlyf

Reputation: 197

request.post() callback doens't work in Google Cloud Functions

So I'm trying to use Express on Google Cloud Functions to get an access token for the Spotify API using the Authorization Code Flow but the callback fails with code 500 and this response body: Error: could not handle the request but when I try to run it locally with firebase serve --only hosting,functions it works fine. This is my code:

const functions = require('firebase-functions');
let express = require('express')
let request = require('request')
let querystring = require('querystring')

let app = express()
const spotifyClientID = "XXX"
const spotifyClientSecret = "XXX"

let redirect_uri = 'http://us-central1-spoti-stats.cloudfunctions.net/app/callback'

app.get('/login', (req, res) => {
  res.redirect('https://accounts.spotify.com/authorize?' +
    querystring.stringify({
      response_type: 'code',
      client_id: spotifyClientID,
      scope: 'user-read-private user-read-email user-top-read playlist-read-private playlist-modify-public',
      redirect_uri
    }))
})

app.get('/callback', (req, res) => {
  let code = req.query.code || null
  let authOptions = {
    url: 'https://accounts.spotify.com/api/token',
    form: {
      code: code,
      redirect_uri,
      grant_type: 'authorization_code'
    },
    headers: {
      'Authorization': 'Basic ' + (new Buffer(spotifyClientID + ":" + spotifyClientSecret).toString("base64"))
    },
    json: true
  }
  request.post(authOptions, (error, response) => { //<-- I'm pretty sure it fails here at this callback
    var access_token = response.body.access_token
    var refresh_token = response.body.refresh_token
    let uri = 'https://spoti-stats.web.app/'
    res.redirect(uri + '?access_token=' + access_token + "&refresh_token=" + refresh_token)
  })
})

console.log(`Go to http://localhost:5000/login .`)

exports.app = functions.https.onRequest(app)

and this is the log from the Firebase console:

TypeError: Cannot read property 'body' of undefined
    at Request.request.post [as _callback] (/srv/index.js:37:33)
    at self.callback (/srv/node_modules/request/request.js:185:22)
    at emitOne (events.js:116:13)
    at Request.emit (events.js:211:7)
    at Request.onRequestError (/srv/node_modules/request/request.js:881:8)
    at emitOne (events.js:116:13)
    at ClientRequest.emit (events.js:211:7)
    at TLSSocket.socketErrorListener (_http_client.js:401:9)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)

(I also tried using body in the callback as a parameter but it returns basically the same error)

Upvotes: 0

Views: 97

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317487

Outgoing HTTP requests are not supported on the Spark plan, in order to prevent unbilled network abuse. You will have to upgrade to a paid plan in order to use the Spotify API.

If you want to learn how to check for errors, you should consult the documentation for the API you're using. It looks like you're just using the standard request module. There is an error object right there in your code - it should be trivial to figure out how to check it.

Upvotes: 1

Related Questions