api 2.0, oauth, and patch....

mattdmmattdm Big grinsPosts: 14Registered Users Big grins
I'm trying to hack up the code from https://api.smugmug.com/api/v2/doc/tutorial/oauth/non-web.html to do some bulk manipulation on my photos. I used it to obtain a Full/Modify access token and secret, and then made a modified copy of the code which just uses that rather than requesting another one.

This works great for `get`. But I'm trying to use `patch` to modify the caption, and I get

{'Code': 401, 'Message': 'Unauthorized'}


Is there example code for this somewhere?

UPDATE: fixed! Problem was that Content-Type: application/json needs to be given explicitly for PATCH. See below for details.

Comments

  • mattdmmattdm Big grins Posts: 14Registered Users Big grins
    edited November 25, 2015
    Here is a snippet of test code. I added the token and and secret previously obtained to the config.json.
    
    #!/usr/bin/env python3
    from rauth import OAuth1Session
    import sys
    import json
    
    NEW_CAPTION_FOR_TEST="Test caption!"
    
    
    API_ORIGIN = 'https://api.smugmug.com'
    
    with open('config.json', 'r') as fh:
        config = json.load(fh)
    
    session = OAuth1Session(
            consumer_key=config["key"],
            consumer_secret=config["secret"],
            access_token=config["token"],
            access_token_secret=config["tokensecret"])
    
    imageresponse=json.loads(session.get(API_ORIGIN+'/api/v2/image/4JpcCNz-0',
                                         headers={'Accept': 'application/json'}).text)
    if imageresponse['Code'] != 200:
        print(imageresponse['Message'])
        sys.exit(2)
    
    image=imageresponse['Response']['Image']
    print("Title:   ", '"' + image['Title'] + '"')
    print("Caption: ", '"' + image['Caption'] + '"')
    print("Can edit?", image['CanEdit'])
    
    changecaptionresponse=json.loads(session.patch(API_ORIGIN + '/api/v2/image/4JpcCNz-0',
                                                   data={"Caption": NEW_CAPTION_FOR_TEST},
                                                   headers={'Accept': 'application/json'}).text)
    
    if changecaptionresponse['Code'] == 200:
        print("Caption changed.")
    else:
        print(changecaptionresponse)
        sys.exit(3)
    
    

    What am I doing wrong?
  • mattdmmattdm Big grins Posts: 14Registered Users Big grins
    edited November 25, 2015
    I should add that I know that the key, secret, token, and token secret are all correct, because if I change any one of them, the read access fails (in various different ways.)
  • mattdmmattdm Big grins Posts: 14Registered Users Big grins
    edited November 25, 2015
    HA! Fixed it. I needed to explicitly add Content-Type: application/json to the headers: specifically,
    changecaptionresponse=json.loads(session.patch(API_ORIGIN + '/api/v2/image/4JpcCNz-0',
                                                   data='{"Caption": "' + NEW_CAPTION_FOR_TEST + '"}',
                                                   headers={'Content-Type' : 'application/json', 'Accept': 'application/json'}).text)
    
    


    I discovered this by switching in desperation from rauth to oauthlib (https://github.com/idan/oauthlib), which is basically a drop-in replacement in this simple case, with a few terminology changes — but for some reason it gave me a helpful message, explaining that "application/x-www-form-urlencoded cannot be used when the HTTP method is PATCH.", which lead me to the answer. I don't know why I don't get this helpful error with rauth. In any case, I first made it work with oauthlib, and then went back to rauth, and — it works too!
  • SivaAnnaSivaAnna Big grins Posts: 9Registered Users Big grins

    Hello need some help :

    I am trying to follow the above examples and want to update the privacy setting for few of the albums to unlisted

    session.patch('https://api.smugmug.com/api/v2/album/XXXXX',
                                                data={"Privacy": 2),
                                                headers={'Content-Type' : 'application/json', 'Accept': 'application/json'}).
    

    For some reason, I am getting the below error :

    {"Code":400,"Message":"Invalid JSON: Syntax error"}

    Looks like the issue with the data field - but i am not able to figure out.

    Any help would be appreciated.

    I have close to 100+ to be updated with the correct settings,

    Thanks in advance.
    Siva

  • gibertigiberti SmugMug Sorcerer Posts: 16Registered Users Big grins

    @SivaAnna said:
    Hello need some help :

    I am trying to follow the above examples and want to update the privacy setting for few of the albums to unlisted

    session.patch('https://api.smugmug.com/api/v2/album/XXXXX',
                                                data={"Privacy": 2),
                                                headers={'Content-Type' : 'application/json', 'Accept': 'application/json'}).
    

    For some reason, I am getting the below error :

    {"Code":400,"Message":"Invalid JSON: Syntax error"}

    Looks like the issue with the data field - but i am not able to figure out.

    Any help would be appreciated.

    I have close to 100+ to be updated with the correct settings,

    Thanks in advance.
    Siva

    Siva, you need to pass the data as a JSON string, your code is presenting an object.

    APIv2 and OAuth are your friend! Having issues? Just ask, I can help!
Sign In or Register to comment.