Search code examples
javascriptmagentooauthmagento-rest-api

javascript xmlhttprequest Authorization magento oauth


I'm cant understand how to user authorization headers in vanila javascript.

guide magento rest-api (oAuth 1.0), https://github.com/nickvane/Magento-RestApi/wiki/Authentication-steps.

how i implement this headers in my code: POST /oauth/initiate

Authorization: OAuth oauth_callback="http%3A%2F%2Flocalhost%3A8888",oauth_consumer_key="YOUR-CONSUMER-KEY",oauth_nonce="t02auly6elcuthly",oauth_signature="7FMe1UducbDUgCJWQY4Avv3g3f4%3D",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1371098086",oauth_version="1.0"

my code:

var ajax = new XMLHTTPREQUEST();
    ajax.open('POST', myurl+'/oauth/initiate',true);
    ajax.send();
    ajax.onreadystatechange = function(){
          if(ajax.readyState == 4 ){

             console.log(ajax.responseText);
          }
    }

how i can add headers like this to my code?


Solution

  • Please read about set request header: XMLHttpRequest.setRequestHeader

    XMLHttpRequest.setRequestHeader(header, value)
    

    you can use XMLHttpRequest.setRequestHeader API to set header for any request.

    For OAuth you can read more: OAuth.

    In your case to implement OAuth to get the token

    POST /oauth/initiate

    The following request parameters should be present in the Authorization header:

    oauth_callback - an URI to which the Service Provider will redirect the resource owner (user) after the authorization is complete.
    oauth_consumer_key - the Consumer Key value, retrieved after the registration of the application.
    oauth_nonce - a random value, uniquely generated by the application.
    oauth_signature_method - name of the signature method used to sign the request. Can have one of the following values: HMAC-SHA1, RSA-SHA1, and PLAINTEXT.
    oauth_signature - a generated value (signature).
    oauth_timestamp - a positive integer, expressed in the number of seconds since January 1, 1970 00:00:00 GMT.
    oauth_version - OAuth version.
    

    Good Luck! :)