How to Trigger an Action When a New Row is Added in Google Sheets and Use Track Name and Artist Name to Search TrackID in Spotify with Node.js?

change this code from clientID and client secret to link spotify account

import { axios } from "@pipedream/platform";

export default defineComponent({
  async run({ steps }) {
    const trackName = steps.trigger.event.newRow[1];
    const artistName = steps.trigger.event.newRow[4];

    const clientId = "clientID";
    const clientSecret = "clientSecret";
    const auth = Buffer.from(`${clientId}:${clientSecret}`).toString("base64");

    const tokenResponse = await axios(this, {
      method: "POST",
      url: "https://accounts.spotify.com/api/token",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": `Basic ${auth}`,
      },
      data: "grant_type=client_credentials",
    });

    const accessToken = tokenResponse.access_token;

    const searchResponse = await axios(this, {
      method: "GET",
      url: "https://api.spotify.com/v1/search",
      headers: {
        "Authorization": `Bearer ${accessToken}`,
      },
      params: {
        q: `track:${trackName} artist:${artistName}`,
        type: "track",
      },
    });

    const trackId = searchResponse.tracks.items[0].id;
    return { trackId };
  },
});

To modify the code to use a linked Spotify account, you can follow these steps: 1. Add a spotify prop to the component. 2. Replace the token generation code with the OAuth access token from the linked Spotify account. 3. Update the Authorization header in the search request. Here’s the updated code: ```
import { axios } from “@pipedream/platform”; export default defineComponent({ props: { spotify: { type: “app”, app: “spotify”, }, }, async run({ steps }) { const trackName = steps.trigger.event.newRow[1]; const artistName = steps.trigger.event.newRow[4]; // Use the OAuth access token from the linked Spotify account const accessToken = this.spotify.$auth.oauth_access_token; const searchResponse = await axios(this, { method: “GET”, url: “https://api.spotify.com/v1/search”, headers: { “Authorization”: Bearer ${accessToken}, }, params: { q: track:${trackName} artist:${artistName}, type: “track”, }, }); const trackId = searchResponse.tracks.items[0].id; return { trackId }; }, });

i got this error
TypeError
Cannot read property ‘id’ of undefined
DETAILS

    at Object.run (file:///tmp/__pdg__/dist/code/f4737dc552eaf5f0776659068a5808363db39e58cd4667c80cd22115d0345994/component.mjs:29:52)
    at process.processTicksAndRejections (internal/process/task_queues.js:95:5)
    at global.executeComponent (/var/task/launch_worker.js:225:22)
    at MessagePort.messageHandler (/var/task/launch_worker.js:722:28)

I apologize for the error you encountered. It seems that the searchResponse.tracks.items[0] object is undefined, which means the search results might be empty or the response structure is different than expected. To handle this case, let’s add a check for the existence of the object before accessing its id property: import { axios } from "@pipedream/platform"; export default defineComponent({ async run({ steps }) { const trackName = steps.trigger.event.newRow[1]; const artistName = steps.trigger.event.newRow[4]; const accessToken = this.spotify.$auth.oauth_access_token; const searchResponse = await axios(this, { method: "GET", url: "https://api.spotify.com/v1/search", headers: { "Authorization": `Bearer ${accessToken}`, }, params: { q: `track:${trackName} artist:${artistName}`, type: "track", }, }); if (searchResponse.tracks.items.length > 0) { const trackId = searchResponse.tracks.items[0].id; return { trackId }; } else { console.log("No track found for the given search query."); return { message: "No track found for the given search query." }; } }, }); This code checks if there are any items in the search results before accessing the id property. If no items are found, it logs a message and returns a message indicating that no track was found.

I got this error when apply your code
TypeError
Cannot read property ‘$auth’ of undefined
DETAILS

    at Object.run (file:///tmp/__pdg__/dist/code/b5f308ca849bb8f5278f83b9666296f0ff6ac7b06e35c67fed2e93a3df1c5e33/component.mjs:8:38)
    at global.executeComponent (/var/task/launch_worker.js:225:53)
    at MessagePort.messageHandler (/var/task/launch_worker.js:722:28)

I apologize for the oversight in my previous response. It seems I missed adding the props section to the component code, which includes the spotify app prop. Here’s the corrected code: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { spotify: { type: "app", app: "spotify", }, }, async run({ steps }) { const trackName = steps.trigger.event.newRow[1]; const artistName = steps.trigger.event.newRow[4]; const accessToken = this.spotify.$auth.oauth_access_token; const searchResponse = await axios(this, { method: "GET", url: "https://api.spotify.com/v1/search", headers: { "Authorization": `Bearer ${accessToken}`, }, params: { q: `track:${trackName} artist:${artistName}`, type: "track", }, }); if (searchResponse.tracks.items.length > 0) { const trackId = searchResponse.tracks.items[0].id; return { trackId }; } else { console.log("No track found for the given search query."); return { message: "No track found for the given search query." }; } }, }); This code now includes the props section with the spotify app prop, which should resolve the error you encountered.