0

as a beginner, I have some problems in using Ajax (with Discogs API) .. to get a discogs request token, discogs is saying

Include the following headers with your request:
Content-Type: application/x-www-form-urlencoded
Authorization:
OAuth oauth_consumer_key="your_consumer_key",
oauth_nonce="random_string_or_timestamp",
oauth_signature="your_consumer_secret&",
oauth_signature_method="PLAINTEXT",
oauth_timestamp="current_timestamp",
oauth_callback="your_callback"
User-Agent: some_user_agent

https://www.discogs.com/developers#page:authentication,header:authentication-discogs-auth-flow

but, how to write this header? below is my trying code, but I know this is not proper.

$.ajax({
    type: "GET",
    url: "https://api.discogs.com/oauth/request_token",
    dataType: 'jsonp',
    headers: {
        ContentType: "application/x-www-form-urlencoded",
        Authorization: OAuth oauth_consumer_key="your_consumer_key",
            oauth_nonce="random_string_or_timestamp",
            oauth_signature="your_consumer_secret&",
            oauth_signature_method="PLAINTEXT",
            oauth_timestamp="current_timestamp",
            oauth_callback="your_callback",
        UserAgent: some_user_agent,
    }
    success: function (data) {
        console.log(data);
        document.getElementById("content").innerHTML += "<br>" + `${data}`;
    },
    error: function (error) {
        console.log(error);
    }
});
HAPPY
  • 25
  • 4

1 Answers1

0

You said:

dataType: 'jsonp',

It isn't possible to specify headers for JSONP requests.

The API can't be using JSONP. Set the dataType to the format they are using.


The documentation says:

When you create a new application, you’ll be granted a Consumer Key and Consumer Secret, which you can plug into your application and start making authenticated requests. It’s important that you don’t disclose the Consumer Secret to anyone.

Putting those in your client-side code will disclose them to all your visitors.

The request to that end point should be made from server-side code.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • if it is not bothering you, can I ask one more? what "dataType" commonly selected, when I do send headers? I searched many examples, but only confusion is being added up. – HAPPY Oct 02 '20 at 01:25
  • The data type needs to be tailored for the type of data you expect to get back from the API; it isn’t a popularity contest. You send headers when you need to send headers (but only when you are using XHR or fetch, it isn’t possible with JSONP). – Quentin Oct 02 '20 at 09:44