Getting 401 Unauthorized Trying to Create Wiki Pages or Characters

Shammond42
Shammond42
edited October 2011 in API Discussion
I'm wondering if anybody has got the create method for wiki pages or characters working. When I try, I get the following errors

#
Invalid OAuth Request

Normally 401 seems to be used for HTTP basic authenitcation. Additionally I seem to have access, because I can do a get request for anything I want, and I can update pages and characters with put requests. Creating new ones with post is the problem. Here is my code.

page_hash = {
'name' => 'Test Page',
'body' => 'Test Body',
'game_master_info' => 'Secret sauce',
'is_game_master_only' => false
}

puts campaign.id
campaign.wiki_pages.each{|page| puts "C: #{page.campaign.id}, P: #{page.id}"}
url = "/v1/campaigns/#{campaign.id}/wikis.json"
puts url
puts page_hash.to_json
@response = MageHand::get_client.access_token.post(url, {'wiki_page' => page_hash}.to_json)

puts @response.inspect
puts @response.body

If anybody has code that works, in any language, I would love to see it.

Thanks,
Steve

Comments

  • Ragnarol
    Ragnarol
    Posts: 16
    Hi,

    I have the create wiki method working in my current code, for the error you are getting I would say it has to do with OAuth authentication, are other methods working?

    You can look at the way I do it (C#) in the NObisidian code:
    http://nobisidian.codeplex.com/

    Good luck!

    P.S. Do you have the create character working? I get an annoying error about author missing/not belonging to the campaign, and maybe you can help me.
  • Shammond42
    Shammond42
    Posts: 65
    Thanks for the info. I looked quickly at your code and realized it is going to take me some time to try to figure out what you are doing differently.

    I don't think it is the OAuth. I can get and put things including wiki pages and characters, but I can create either with a post.

    Steve
  • Shammond42
    Shammond42
    Posts: 65
    I'm happy to try to help you create characters. Can you post more information about the error you are getting and the URL you are posting to?

    Thanks,
    Steve
  • Ragnarol
    Ragnarol
    Posts: 16
    Uhm, maybe the user you are using doesn't have rights to create wiki pages? I suppose it has, but lets start by the basics ;)

    If you want I can post here an example of the JSON I use for create wikis, the URL and HTTP headers.

    You can find the details of my problems with the create character here

    http://forums.obsidianportal.com/comments.php?DiscussionID=2143&page=1#Item_2

    I think if you can post a JSON example of a creation it will be enough... has to be that I am missing a node...
  • Shammond42
    Shammond42
    Posts: 65
    If you can post the json you use for a create, the URL and headers that would be very helpful. I'll be happy to post my character create json when I get it working. Right now I still get the same 401 error for both characters and wiki's.

    If you want I'm happy to post the JSON I think I want to send for a character, but I don't actually know that it works.

    Steve
  • Ragnarol
    Ragnarol
    Posts: 16
    A create Wiki example.

    URL: http://api.obsidianportal.com/v1/campaigns/CAMPAIGNID/wikis.json

    JSON: {"wiki_page":{"name":"Probando2"}}
    HTTP METHOD: POST
    HTTP HEADERS:
    Authorization: OAuth oauth_nonce=3558505, oauth_signature_method=HMAC-SHA1, oauth_timestamp=1319925845, oauth_consumer_key=APP_CONSUMER_KEY, oauth_token=vVYw3jfRJOl3t1Bq2aCH, oauth_signature=csQ1LpZVL4qeQeyinXWuVcesQpM%3D, oauth_version=1.0

    Hope that helps!
  • Shammond42
    Shammond42
    Posts: 65
    Finally got power back on after the storm.

    In Googling around, I've read that out of sync clocks can cause the 401 issue. My clock seems right, but I don't know how to check OP. However, I think I can assume that if you have it working their clock is OK.

    Here are my headers.

    authorization: OAuth oauth_body_hash="3f4mFOy5QWwZCjZkpfhYSEuNkho%3D", oauth_consumer_key=APP_CONSUMER_KEY, oauth_nonce="wr2QZgNSBZ6gGYcJP9xGVZ1QzM5wwcQB0hAQgB9O4Q0", oauth_signature="kojXARjrOJ%2BC1sC6QaTFWxTZbD8%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1320147883", oauth_token="2SuIzXKC8ikQ875bDEnU", oauth_version="1.0"

    The differences I see are that you don't have an oauth_body_hash, and my oauth_nonce looks a lot different than yours. I'm going to do a little research into the nonce, that sticks out. However, it seems odd that I can do gets and puts if my headers are off...

    Steve
  • Ragnarol
    Ragnarol
    Posts: 16
    I remembered getting lots of 401 in my first testings, but I addressed them making sure the timestamp is regenerated before each request, as well as the nonce.
    Have you checked if you are reusing them?

    Also I got them when using bad generated signatures. You can try using the following tools to test your signature, they were quite helpful to me:

    http://support.mashery.com/docs/tips_and_tricks/oAuth

    Apart from that your headers look ok :S
  • Shammond42
    Shammond42
    Posts: 65
    Just to confirm. You are NOT sending a body_hash as part of your OAuth headers on post?
  • Ragnarol
    Ragnarol
    Posts: 16
    Confirmed, I am NOT sending it.
  • Shammond42
    Shammond42
    Posts: 65
    Damn, I got it to work. I needed to set my content type to application/x-www-form-urlencoded, which tricks my OAuth library (ruby OAuth) into not sending the body_hash header.

    That seems to be a terrible hack, and I suspect a problem on the OP side of things. Let me clean up a little bit of code, then I will try to create a character and get some JSON for you. Then, I'll explore my "fix" some more.

    Thanks for the help,
    Steve
  • Ragnarol
    Ragnarol
    Posts: 16
    Awesome! Glad to hear you sorted it out :)

    Ansious to get that JSON ;)
  • Shammond42
    Shammond42
    Posts: 65
    Not what you want to hear, but I can reproduce your issue.

    {"character":{"name":"Test Dude","author":{"username":"shammond42","id":"79dae596f24211dfba8140403656340d","profile_url":"http://www.obsidianportal.com/profile/shammond42"}}}

    #
    {"errors":[{"code":4010,"message":"Author can't be blank"},{"code":4010,"message":"Author is not a member of that campaign"}],"http_status":400}

    I tried using author-id and author sub-hash with just the id in it. None of those worked, but I'm sure you knew that.

    Steve
  • Ragnarol
    Ragnarol
    Posts: 16
    Ouch!

    Well, lets see if now that we are two we get any response from the obsidian guys :S

    I will let you know If I am able to make it work, please do you the same ;)
  • Shammond42
    Shammond42
    Posts: 65
    I got it. Here is the json that worked.

    {"character":{"name":"Test Dude","author_id":"79dae596f24211dfba8140403656340d"}}

    Steve
  • Ragnarol
    Ragnarol
    Posts: 16
    Cool! Thanks man, I will make the changes and let you know if it worked for me!
  • Ragnarol
    Ragnarol
    Posts: 16
    Tried it just now and it works perfectly. Thanks Steve!
Sign In or Register to comment.

March 2024
Wrath of the Highborn

Read the feature post on the blog
Return to Obsidian Portal

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Discussions