admin管理员组

文章数量:1313121

Steps to reproduce error: /

I have been following the above tutorial for sending an API request to Spotify. I am successful in getting the authorisation codes, however, Postman has an error when running the collection for Spotify despite following the tutorial correctly.

From looking at the console, there is an error at the line shown below: Error message reads XPath runner/search already exists Error running scripts: JSONError | Unexpected token u in JSON at position 0 Error executing pre-request scripts for Get Artist Top Tracks: JSONError: Unexpected token u in JSON at position 0 Two others experienced the issue but they were never replied to. Any ideas how to fix this? I am new to API requests so any help would be muchly appreciated!

Pre request script for auth:

var client_credentials = (environment.client_id + ':' + environment.client_secret).encodeBase64();
postman.setEnvironmentVariable("client_credentials", client_credentials);

environment.cloned_environment && (delete environment.cloned_environment);
var environmentObjClone = _.cloneDeep(environment);
postman.setEnvironmentVariable("cloned_environment", JSON.stringify(environmentObjClone));

Pre-Request Script for artist:

 try {
    var artist_id = _.sample(JSON.parse(environment.artists));
    postman.setEnvironmentVariable("id", artist_id);
}
catch (e) {
    console.log(e);
    throw false;
}

JSON:

  {
        "id": "b2b45667-7ec9-4d5d-9e38-c976d181c2bd",
        "name": "SpotifyGenV1.template1",
        "values": [
            {
                "key": "artists",
                "value": "[“2DaxqgrOhkeH0fpeiQq2f4”, “2ooIqOf4X2uz4mMptXCtie”]",
                "enabled": true
            },
            {
                "key": "N",
                "value": "5",
                "enabled": true
            },
            {
                "key": "user_id",
                "value": "marcz2007",
                "enabled": true
            },
            {
                "key": "country_code",
                "value": "US",
                "enabled": true
            },
            {
                "key": "client_id",
                "value": "744a1316f9374a27b7ec9bc7eabb0f92",
                "enabled": true
            },
            {
                "key": "client_secret",
                "value": "---secret--",
                "enabled": true
            },
            {
                "key": "refresh_token",
                "value": "AQAf0rvuNBo23wiIiyWQRfavFvx7JdTjvGYLufOBAM1ABtvXWvfqqo_I0IfhFAelTPIh8w2cscS4C1k8QLKOc1rqn18CjKJbmczfu1X-EfEFaWjcDbIPYGCIGZmbNZv5DgiJ9w",
                "enabled": true
            },
            {
                "key": "client_credentials",
                "value": "NzQ0YTEzMTZmOTM3NGEyN2I3ZWM5YmM3ZWFiYjBmOTI6M2ZiNWM2MDBkODRhNDU1ODhmZTNiZWM4MTI5N2E4M2Y=",
                "enabled": true
            },
            {
                "key": "cloned_environment",
                "value": "{\"artists\":\"[“2DaxqgrOhkeH0fpeiQq2f4”, “2ooIqOf4X2uz4mMptXCtie”]\",\"N\":\"5\",\"user_id\":\"marcz2007\",\"country_code\":\"US\",\"client_id\":\"744a1316f9374a27b7ec9bc7eabb0f92\",\"client_secret\":\"3fb5c600d84a45588fe3bec81297a83f\",\"refresh_token\":\"AQAf0rvuNBo23wiIiyWQRfavFvx7JdTjvGYLufOBAM1ABtvXWvfqqo_I0IfhFAelTPIh8w2cscS4C1k8QLKOc1rqn18CjKJbmczfu1X-EfEFaWjcDbIPYGCIGZmbNZv5DgiJ9w\",\"client_credentials\":\"NzQ0YTEzMTZmOTM3NGEyN2I3ZWM5YmM3ZWFiYjBmOTI6M2ZiNWM2MDBkODRhNDU1ODhmZTNiZWM4MTI5N2E4M2Y=\",\"authorization\":\"BQD6I914g3iIt3zs1lpZl7nriwNgkFNMuIjIPjEa6ElTZw_PEbx3ewZkb38zlhypR5BfuyXLUxdJ0WyZ1TWETMSPYpPwBk7Olf6E5J49Q67BhgFWp5A4HyxYHgXNO-56sbOGx1aszETJ24Q8uGfOizzsv9WhpVU2cuC8lJdA3K2aYqc0L-8JnmD0VIrQhwX0\",\"playlist_id\":\"0cfptAtDW6jHMeWauwQ13w\",\"artist_name\":\"Oasis\",\"artistID\":\"2DaxqgrOhkeH0fpeiQq2f4\",\"trackID\":\"4AKUOaCRcoKTFnVI9LtsrN\"}",
                "enabled": true
            },
            {
                "key": "authorization",
                "value": "BQD6I914g3iIt3zs1lpZl7nriwNgkFNMuIjIPjEa6ElTZw_PEbx3ewZkb38zlhypR5BfuyXLUxdJ0WyZ1TWETMSPYpPwBk7Olf6E5J49Q67BhgFWp5A4HyxYHgXNO-56sbOGx1aszETJ24Q8uGfOizzsv9WhpVU2cuC8lJdA3K2aYqc0L-8JnmD0VIrQhwX0",
                "enabled": true
            },
            {
                "key": "playlist_id",
                "value": "0cfptAtDW6jHMeWauwQ13w",
                "enabled": true
            },
            {
                "key": "artist_name",
                "value": "Oasis",
                "enabled": true
            },
            {
                "key": "artist_id",
                "value": "2DaxqgrOhkeH0fpeiQq2f4",
                "enabled": true
            },

        ],
        "_postman_variable_scope": "environment",
        "_postman_exported_at": "2019-07-28T12:45:10.195Z",
        "_postman_exported_using": "Postman/7.3.4"
    }

Steps to reproduce error: https://blog.getpostman./2016/11/09/generate-spotify-playlists-using-a-postman-collection/

I have been following the above tutorial for sending an API request to Spotify. I am successful in getting the authorisation codes, however, Postman has an error when running the collection for Spotify despite following the tutorial correctly.

From looking at the console, there is an error at the line shown below: Error message reads XPath runner/search already exists Error running scripts: JSONError | Unexpected token u in JSON at position 0 Error executing pre-request scripts for Get Artist Top Tracks: JSONError: Unexpected token u in JSON at position 0 Two others experienced the issue but they were never replied to. Any ideas how to fix this? I am new to API requests so any help would be muchly appreciated!

Pre request script for auth:

var client_credentials = (environment.client_id + ':' + environment.client_secret).encodeBase64();
postman.setEnvironmentVariable("client_credentials", client_credentials);

environment.cloned_environment && (delete environment.cloned_environment);
var environmentObjClone = _.cloneDeep(environment);
postman.setEnvironmentVariable("cloned_environment", JSON.stringify(environmentObjClone));

Pre-Request Script for artist:

 try {
    var artist_id = _.sample(JSON.parse(environment.artists));
    postman.setEnvironmentVariable("id", artist_id);
}
catch (e) {
    console.log(e);
    throw false;
}

JSON:

  {
        "id": "b2b45667-7ec9-4d5d-9e38-c976d181c2bd",
        "name": "SpotifyGenV1.template1",
        "values": [
            {
                "key": "artists",
                "value": "[“2DaxqgrOhkeH0fpeiQq2f4”, “2ooIqOf4X2uz4mMptXCtie”]",
                "enabled": true
            },
            {
                "key": "N",
                "value": "5",
                "enabled": true
            },
            {
                "key": "user_id",
                "value": "marcz2007",
                "enabled": true
            },
            {
                "key": "country_code",
                "value": "US",
                "enabled": true
            },
            {
                "key": "client_id",
                "value": "744a1316f9374a27b7ec9bc7eabb0f92",
                "enabled": true
            },
            {
                "key": "client_secret",
                "value": "---secret--",
                "enabled": true
            },
            {
                "key": "refresh_token",
                "value": "AQAf0rvuNBo23wiIiyWQRfavFvx7JdTjvGYLufOBAM1ABtvXWvfqqo_I0IfhFAelTPIh8w2cscS4C1k8QLKOc1rqn18CjKJbmczfu1X-EfEFaWjcDbIPYGCIGZmbNZv5DgiJ9w",
                "enabled": true
            },
            {
                "key": "client_credentials",
                "value": "NzQ0YTEzMTZmOTM3NGEyN2I3ZWM5YmM3ZWFiYjBmOTI6M2ZiNWM2MDBkODRhNDU1ODhmZTNiZWM4MTI5N2E4M2Y=",
                "enabled": true
            },
            {
                "key": "cloned_environment",
                "value": "{\"artists\":\"[“2DaxqgrOhkeH0fpeiQq2f4”, “2ooIqOf4X2uz4mMptXCtie”]\",\"N\":\"5\",\"user_id\":\"marcz2007\",\"country_code\":\"US\",\"client_id\":\"744a1316f9374a27b7ec9bc7eabb0f92\",\"client_secret\":\"3fb5c600d84a45588fe3bec81297a83f\",\"refresh_token\":\"AQAf0rvuNBo23wiIiyWQRfavFvx7JdTjvGYLufOBAM1ABtvXWvfqqo_I0IfhFAelTPIh8w2cscS4C1k8QLKOc1rqn18CjKJbmczfu1X-EfEFaWjcDbIPYGCIGZmbNZv5DgiJ9w\",\"client_credentials\":\"NzQ0YTEzMTZmOTM3NGEyN2I3ZWM5YmM3ZWFiYjBmOTI6M2ZiNWM2MDBkODRhNDU1ODhmZTNiZWM4MTI5N2E4M2Y=\",\"authorization\":\"BQD6I914g3iIt3zs1lpZl7nriwNgkFNMuIjIPjEa6ElTZw_PEbx3ewZkb38zlhypR5BfuyXLUxdJ0WyZ1TWETMSPYpPwBk7Olf6E5J49Q67BhgFWp5A4HyxYHgXNO-56sbOGx1aszETJ24Q8uGfOizzsv9WhpVU2cuC8lJdA3K2aYqc0L-8JnmD0VIrQhwX0\",\"playlist_id\":\"0cfptAtDW6jHMeWauwQ13w\",\"artist_name\":\"Oasis\",\"artistID\":\"2DaxqgrOhkeH0fpeiQq2f4\",\"trackID\":\"4AKUOaCRcoKTFnVI9LtsrN\"}",
                "enabled": true
            },
            {
                "key": "authorization",
                "value": "BQD6I914g3iIt3zs1lpZl7nriwNgkFNMuIjIPjEa6ElTZw_PEbx3ewZkb38zlhypR5BfuyXLUxdJ0WyZ1TWETMSPYpPwBk7Olf6E5J49Q67BhgFWp5A4HyxYHgXNO-56sbOGx1aszETJ24Q8uGfOizzsv9WhpVU2cuC8lJdA3K2aYqc0L-8JnmD0VIrQhwX0",
                "enabled": true
            },
            {
                "key": "playlist_id",
                "value": "0cfptAtDW6jHMeWauwQ13w",
                "enabled": true
            },
            {
                "key": "artist_name",
                "value": "Oasis",
                "enabled": true
            },
            {
                "key": "artist_id",
                "value": "2DaxqgrOhkeH0fpeiQq2f4",
                "enabled": true
            },

        ],
        "_postman_variable_scope": "environment",
        "_postman_exported_at": "2019-07-28T12:45:10.195Z",
        "_postman_exported_using": "Postman/7.3.4"
    }
Share Improve this question edited Jul 28, 2019 at 13:10 marcz2007 asked Jul 28, 2019 at 10:24 marcz2007marcz2007 1671 gold badge1 silver badge13 bronze badges 3
  • As you're getting a 400 error is probably something wrong in your code. Can you share an image of what you have in your pre-request script for those requests. The blog was from 3 years ago so there is also a possibility that Spotify has also change since then. I would suspect from that error it's to do with parsing the request body. – Danny Dainton Commented Jul 28, 2019 at 11:15
  • Yes that could be it, I've editted in the pre request scripts above (which was empty for the api.spotify./v1/artists{{id}}/related-artists one). Anything catch your eye as being wrong here? – marcz2007 Commented Jul 28, 2019 at 13:12
  • I'm having the same issue and what's really weird is that this same call used to work. I even did a demo on it and havent' changed the code since. And yes, I am set to the same environment as before. – Dan Csharpster Commented Feb 6, 2022 at 16:01
Add a ment  | 

2 Answers 2

Reset to default 2

I haven't read your question in detail, but JSONError: Unexpected token u in JSON at position 0 is the typical error message when you try to GET a Postman variable that doesn't exist. (Because of misspelling or whatever.)

Closely related is the error message JSONError: Unexpected token '<' at 1:1<!doctype html>^ which typically occurs when you try to request a URL that doesn't exist.

A missing environment variable is one cause. Also ensure that the value is present in the Initial Value section and not just the Current Value

Another is accidently leaving a return character in a variable which will give the following

unexpected token \n in JSON...

Remove the return character (postman UI shows it as the return symbol and not the character itself) and it resolves this issue.

本文标签: