question

Dominic Bilke avatar image
Dominic Bilke asked

Login and Request script doesn't work

Hello,

my Login and Request Script doesn't work with JavaScript.

Can anyone help?

The Login works, and displays the Login window of Amazon. I can get the access code and profile information. But when I want to get the users playlists an error 401 occurs. The users playlists cannot be fetched. The ajax request is the problem...

Can anyone correct my script?

Thanks.

<script type=\"text/javascript\">

    window.onAmazonLoginReady = function() {
      amazon.Login.setClientId('amzn1.application-oa2-client.22ac50...');
    };
    (function(d) {
      var a = d.createElement('script'); a.type = 'text/javascript';
      a.async = true; a.id = 'amazon-login-sdk';
      a.src = 'https://assets.loginwithamazon.com/sdk/na/login1.js';
      d.getElementById('amazon-root').appendChild(a);
    })(document);

    var authCode = '';
    function doLogin() {
        options = {};
        options.scope = 'profile';
        options.pkce = true;
        window.amazon.Login.authorize(options, function(response) {
            if ( response.error ) {
                alert('oauth error ' + response.error);
            return;
            }
            window.amazon.Login.retrieveToken(response.code, function(response) {
                if ( response.error ) {
                    alert('oauth error ' + response.error);
                return;
                }
                authCode = response.access_token;
                window.amazon.Login.retrieveProfile(response.access_token, function(response) {
                    //alert('Hello, ' + response.profile.Name);
                    //alert('Your e-mail address is ' + response.profile.PrimaryEmail);
                    //alert('Your unique ID is ' + response.profile.CustomerId);
                    if ( window.console && window.console.log )
                       window.console.log(response);
$.ajax({
        url: 'https://api.music.amazon.dev/v1/users/'+response.profile.CustomerId+'/playlists?limit=50',
        beforeSend: function(xhr) {
             xhr.setRequestHeader('Authorization', 'Bearer '+authCode)
             xhr.setRequestHeader('x-api-key', 'amzn1.application-oa2-client.22ac...')
        }, success: function(data){
                        data = JSON.parse(data);
                        for (let playlist in data.user.playlists.edges) {
                              let newOption = new Option(playlist.name,playlist.url);
                              let select = document.getElementbyId('amazon_playlist_id'); 
                              select.add(newOption);
                        }

        }
});
                });
            });
        });
   }; 
   setTimeout(window.doLogin, 3000);
</script>


login with amazonrequestjavascript
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

0 Answers