Could not get spotify information when site is deployed

site: https://startling-donut-6eeafd.netlify.app/
function: https://startling-donut-6eeafd.netlify.app/.netlify/functions/spotifyToken

I have created netlify function for getting spotify access token using getSecrets and it works as it should on localhost returning all my spotify information. The problem comes when I deploy the site, because then the function just returns empty object.

Should I do any additional configuration for netlify Graph?

Hi @joze747, if possible can you share a code snippet of your function for me to help with the debugging.
Thanks.

Hi @clarnx. Here is the function code.

import { Handler, getSecrets, NetlifySecrets } from "@netlify/functions"

const handler: Handler = async (event, context) => {
  let secrets: NetlifySecrets = {}
  secrets = await getSecrets(event)
  console.log(secrets)

  if (secrets.spotify) {
    return {
      statusCode: 200,
      body: JSON.stringify({ token: secrets.spotify.bearerToken }),
    }
  } else {
    return {
      statusCode: 200,
      body: JSON.stringify({ message: "Could not get Spotify information." }),
    }
  }
}

export { handler }

HI @joze747, thanks for sharing the snippet. The function code seems okay.

However I want to find out if you have enabled the Spotify API Authentication feature on our Site that you just deployed.

If you have not enabled the feature kindly follow the steps below on how to connect Spotify to your site.

To get started, you have to enable the feature on our Netlify user account.

  1. Head over to the Netlify Labs page at:

You will see Netlify Graph listed under Experimental features.

  1. Click Enable next to that feature if it’s not enabled.

Head back over to the Netlify dashboard, find your newly deployed Site with functions enabled, and then click on your site.

If you look on the top menu, you should see a Graph menu item.

  1. Click on Graph menu item and select the Spotify icon or logo.

  2. Click on the API menu toggle to enable.

  3. After click on login to login with your Spotify account.

  4. After you’ve logged in click on the third icon which is the edit operation icon.

  5. Click on the Site & Authentication option. Kindly read the information on the Site & Authentication options page and then configure it accordingly to your site.

  6. After scroll down to the API Authentication section and then click Choose a service and select Spotify.

Make sure you enable the correct permissions and Scopes so that you can make requests to the API endpoints

  1. After click on Enable to continue the process.

Your Site should now be connected to Spotify and you should now be able to start working with their API!

You can also find more information about connecting to an API service or Graph Authentication in the Official Netlify GitHub page link below.

Let me know if the above suggestions works or not.
Thanks.

HI @clarnx, thank you for your answer.

I’ve already configured everything in your proposed solution. What is weird is that the site used to work correctly until 3 weeks ago and even now works correctly when being run with netlify-cli. I even redeployed the site in a new Netlify project but getSecrets() function didn’t work, so now I am really without any idea what I am doing wrong.

Hi @joze747, thanks for the feedback.
If it’s working on local development and not in production then something must be going wrong with your deployments.

At this point there could be so many reasons causing your current deployment not to work. It would be difficult to point out what might be causing the getSecrets function not to work.

However I just want to find out if you made any changes to your code before the time you stated that it worked correctly?

Secondly note that Authentication tokens are specific to each site. If you enable an API provider for a site, you can’t reuse the token on other sites. You need to authenticate with the same provider again for every site

I recommend you take your time and read the Official Netlify Graph Authentication Documentation again. Sometimes taking a second look at a documentation helps in case you missed something.

Hey there, @joze747 :wave:

When you have a moment, please let us know if you are still encountering this obstacle! @clarnx, thanks so much for all of the thorough suggestions here.

@joze747 if you follow @clarnx’s suggestions and are still having issues, please provide a reproduction for us so that we can look into the details further.

1 Like

Hey @hillary,
I am still having a problem. What do you mean by providing reproduction?

Thank you for your help.

A repository that we can use directly without having to setup an entire project trying to see the error that you’re seeing.

Hi @hillary, I have successfully solved the problem. I removed netlify-cli and updated @netlify/functions to newer version in package.json and I think one of those two was the solution.

1 Like

Hey there,

Thank you so much for coming back and letting us know! This solution will help future Netlify forums members who encounter something similar. Happy building :rocket:

1 Like