PATCH not working. Get 401 error.

dpatel20dpatel20 Registered Users Posts: 3 Beginner grinner
I'm using smugmug API and trying to send a patch request to update an album's password. The below code is a quick trial using Javascript. I use the ddooauth library here : from Github (I can't post links) github / ddo / oauth-1.0a.

It works fine for a GET request but as soon as I try to do a PATCH request it gives me an error 401 (unauthorised). I think I am doing something wrong with the way I form the request. Do the parameters (i.e. the password field and new password) need to be part of the signature hash or should it be separate?

<CODE>
// Initialize
const oauth = OAuth({
consumer: {
key: 'consumer_key',
secret: 'secret'
},
signature_method: 'HMAC-SHA1',
hash_function(base_string, key) {
return CryptoJS.HmacSHA1(base_string, key).toString(CryptoJS.enc.Base64);
}
});

const request_data = {
url: 'ht tps :// api.smugmug.com / api / v2 / album / xhXXXX',
method: 'PATCH',
data: {Password: "temp123"} // Should this be here??
};

// Note: The token is optional for some requests
const token = {
key: 'key',
secret: 'access_token'
};

function loadDoc() {
console.log(oauth.authorize(request_data, token))
$.ajax({
url: request_data.url,
type: request_data.method,
headers: {
Accept : "application/json",
'Content-Type' : 'application/json'

},
data: oauth.authorize(request_data, token)
}).done(function(data) {
// Process your data here
console.log(data)
});
}
</CODE>
Tagged:

Comments

  • quiksquiks Registered Users Posts: 19 Big grins

    Hi,

    Me too, doing an Android app, Having the POST/GET working with Oauth.
    PATCH keeps sending me back "Unable to respond to any of these challenges: {none=WWW-Authenticate: None realm="SmugMug"}".
    It's like Im not even sending any Authorization at all...

                    HttpPatch patch = new HttpPatch("https://api.smugmug.com/api/v2/image/XXXXXXX");
                    patch.addHeader("Content-Type","application/json");
                    patch.addHeader("Accept", "application/json");
                    consumer.sign(patch);
                    String json = "{\"Keywords\": \"edc\"}";
                    HttpEntity entity = new ByteArrayEntity(json.getBytes("UTF-8"));
                    patch.setEntity(entity);
                    response = httpclient.execute(patch);
    

    Anyone? Thanks a lot.

Sign In or Register to comment.