How to request an oauth token?

100 views
Skip to first unread message

zealalot

unread,
Mar 20, 2009, 5:08:56 AM3/20/09
to jaikuengine-discuss
Having trouble requesting an OAuth token. Can anyone here help?
First off, I'm not sure what URL to send the POST request to.
Secondly, do I need to declare a method? Any help greatly
appreciated..

-- code ---

import urllib.request, urllib.parse, time, string, random

class OAuthToken():
"""Simple object for requesting an OAuth token"""
def __init__(self, consumer_key, secret_key):
self.tokreq = {'method': 'authorize'} #no idea what to put for
method
self.tokreq['oauth_consumer_key'] = consumer_key
self.tokreq['oauth_signature_method'] = 'HMAC-SHA1'
self.tokreq['oauth_signature'] = secret_key
self.tokreq['oauth_timestamp'] = str(int(time.time()))
self.tokreq['oauth_nonce'] = random.choice
(string.ascii_letters) #Build random nonce
for i in range(0,15):
self.tokreq['oauth_nonce'] += random.choice
(string.ascii_letters)

def get_token(self, url='http://api.jaiku.com/json/api/
request_token'): #is this the right url?
"""Requests an OAuth token from the specified URL"""
fh = urllib.request.urlopen(url, urllib.parse.urlencode
(self.tokreq))
print(fh.read())

mytoken = OAuthToken('2ff4540897c94f58ae77611eddf1e42c', 'secret')
mytoken.get_token()

Kim Bach

unread,
Mar 20, 2009, 6:08:41 AM3/20/09
to jaikuengine-discuss
I have a similar problem, but I'm using the oauth Python library
http://oauth.googlecode.com/svn/code/python/oauth/ from Google Code,
which frankly seems like overkill for generating HTTP parameters and
headers, but I suppose it might pay off, I also got inspiration from
the "gifts" demo project http://code.google.com/intl/da/apis/opensocial/articles/appengine-0.8.html,
that uses orkut for authentication.

The result no matter what I try, I get the dreaded HTTP Error 500 -
it's possible I'm doing something wrong, but error 500 doesn't exactly
give me a lot to work with.

My Jaiku handle is @kimbach

zealalot

unread,
Mar 20, 2009, 10:38:49 AM3/20/09
to jaikuengine-discuss
Thanks for the reply Kim.

I was wondering, what URL do you send your POST request to? I've
tried http://jaiku.com/api/request_token and http://api.jaiku.com/json.
I get the 'something broke' page on the first, and 'invalid method' on
the second.

Thanks,

zealalot@jaiku

On Mar 20, 6:08 am, Kim Bach <kim.b...@gmail.com> wrote:
> I have a similar problem, but I'm using the oauth Python libraryhttp://oauth.googlecode.com/svn/code/python/oauth/from Google Code,
> which frankly seems like overkill for generating HTTP parameters and
> headers, but I suppose it might pay off, I also got inspiration from
> the "gifts" demo projecthttp://code.google.com/intl/da/apis/opensocial/articles/appengine-0.8...,

Kim Bach

unread,
Mar 20, 2009, 10:51:05 AM3/20/09
to jaikuengine-discuss
I'm connecting to www.jaiku.com on port 80 (it will most likely
resolve to the same IP as jaiku.com), after that I try to access /api/
request_token which is an OAuth best-practice AFAIK

On 20 Mar., 15:38, zealalot <zeala...@gmail.com> wrote:
> Thanks for the reply Kim.
>
> I was wondering, what URL do you send your POST request to?  I've
> triedhttp://jaiku.com/api/request_tokenandhttp://api.jaiku.com/json.
> I get the 'something broke' page on the first, and 'invalid method' on
> the second.
>
> Thanks,
>
> zealalot@jaiku
>
> On Mar 20, 6:08 am, Kim Bach <kim.b...@gmail.com> wrote:
>
> > I have a similar problem, but I'm using the oauth Python libraryhttp://oauth.googlecode.com/svn/code/python/oauth/fromGoogle Code,

zealalot

unread,
Mar 23, 2009, 12:42:43 PM3/23/09
to jaikuengine-discuss
Well, I finally managed to get a request_token! Now if I could only
figure out how to authorize it, and get an access_token. To finally
get the request token, I used 'GET' instead of 'POST' and
'www.jaiku.com' instead of 'jaiku.com'. Here's my code:

-- CODE --
#!/usr/bin/python

import oauth, httplib, urllib

#Define Constants
KEY = 'Your key from the JAIKU API page' #Change Me
SECRET = 'Your secret key' #Change Me
SERVER = 'www.jaiku.com'
REQURL = '/api/request_token'

class MyOAuthClient(oauth.OAuthClient):

def __init__(self, server, request_token_url):
self.server = server
self.request_token_url = request_token_url
self.full_request_url = "http://" + self.server +
self.request_token_url
self.connection = httplib.HTTPConnection(self.server)

def fetch_request_token(self, oauth_request):
self.connection.request('GET', self.request_token_url,
headers=oauth_request.to_header())
response = self.connection.getresponse()
return oauth.OAuthToken.from_string(response.read())

if __name__ == "__main__":
#Initialize
client = MyOAuthClient(SERVER, REQURL)
consumer = oauth.OAuthConsumer(KEY, SECRET)
sig_method = oauth.OAuthSignatureMethod_HMAC_SHA1()

#Request Token
oauth_request = oauth.OAuthRequest.from_consumer_and_token
(consumer, http_url=client.full_request_url)
oauth_request.sign_request(sig_method, consumer, None)
token = client.fetch_request_token(oauth_request)

print "-Token: " + str(token) + " .."

-- CODE --

On Mar 20, 10:51 am, Kim Bach <kim.b...@gmail.com> wrote:
> I'm connecting towww.jaiku.comon port 80 (it will most likely

Kim Bach

unread,
Mar 24, 2009, 4:44:25 AM3/24/09
to jaikuengine-discuss
Thanks that helped me too, now I also get request tokens, and I was
also able to generate the authorize URL (in this format:
http://www.jaiku.com/api/authorize?oauth_token=REQUEST_TOKEN_KEY&perms=PERMISSIONS),
and Jaiku apparently accepted the autorization request, unfortunately
the autorized tokens didn't show up on the list of authorized tokens
(http://www.jaiku.com/api/tokens), and I suspect that that is the
reason the subsequent requests for access tokens fails. I'm trying to
look at the #JaikuEngine source for clues.

It also seems wrong to me that we're served some HTML and HTTP 200 in
case of an error, but I don't know the OAuth best pratice on error
handling.
> > I'm connecting towww.jaiku.comonport 80 (it will most likely

zealalot

unread,
Mar 24, 2009, 9:41:26 PM3/24/09
to jaikuengine-discuss
For me to get an authorized request, I actually ended up building
something like:

'http://jaiku.appspot.com/login?redirect_to=%2Fapi%2Fauthorize
%3Foauth_token%3D' + REQUEST_TOKEN_KEY + '%26perms%3Dwrite'

After that, the token actually showed up in my activated tokens list.

On Mar 24, 4:44 am, Kim Bach <kim.b...@gmail.com> wrote:
> Thanks that helped me too, now I also get request tokens, and I was
> also able to generate the authorize URL (in this format:http://www.jaiku.com/api/authorize?oauth_token=REQUEST_TOKEN_KEY&perm...),
> and Jaiku apparently accepted the autorization request, unfortunately
> the autorized tokens didn't show up on the list of authorized tokens
> (http://www.jaiku.com/api/tokens), and I suspect that that is the
> reason the subsequent requests for access tokens fails. I'm trying to
> look at the #JaikuEngine source for clues.
>
> It also seems wrong to me that we're served some HTML and HTTP 200 in
> case of an error, but I don't know the OAuth best pratice on error
> handling.
>
> On 23 Mar., 17:42, zealalot <zeala...@gmail.com> wrote:
>
> > Well, I finally managed to get a request_token!  Now if I could only
> > figure out how to authorize it, and get an access_token.  To finally
> > get the request token, I used 'GET' instead of 'POST' and
> > 'www.jaiku.com'insteadof 'jaiku.com'.  Here's my code:
> > > I'm connecting towww.jaiku.comonport80 (it will most likely

Kim Bach

unread,
Mar 25, 2009, 9:27:32 AM3/25/09
to jaikuengine-discuss
So far I've had no success getting anything show up in the list of
authorised API tokens (http://www.jaiku.com/api/tokens)

What is the name of your app, I might try using the same appname
> > > > I'm connecting towww.jaiku.comonport80(it will most likely

jade

unread,
Mar 27, 2009, 5:51:37 AM3/27/09
to jaikuengine-discuss
Hi all,

I have the same problem, I can't get the
access token.
After a user authorizes a request token (he clicks
the button 'authorize' in /api/authorize page) he's not redirected
back to my website but he remains in the same page and I can't go on
to ask for an access token.
And also the autorized tokens don't show up on the list of authorized
tokens (http://www.jaiku.com/api/tokens)
I have added to the authorize url the oauth_callback parameter with a
url on my website to let me know that the user has completed
authorizing me and to get the authorized token but it doesn't work

Thanks for any help...
> > > > > I'm connecting towww.jaiku.comonport80(itwill most likely

Kim Bach

unread,
Mar 28, 2009, 2:08:54 AM3/28/09
to jaikuengine-discuss
Progress :-), @zealot will share his code soon:
http://zealalot.jaiku.com/presence/68b39823eb5e4b7d8a126a7af4f51bd9
> > > > > > I'm connecting towww.jaiku.comonport80(itwillmost likely

Kim Bach

unread,
Mar 30, 2009, 6:23:38 AM3/30/09
to jaikuengine-discuss
@zealalot has posted this code. I haven't tried it yet, but I suppose
that it'll work

http://code.google.com/p/zealots-pjs/downloads/list

jade

unread,
Apr 3, 2009, 10:48:01 AM4/3/09
to jaikuengine-discuss
Does @zealalot code work? Have you tried it also with oauth_callback
parameter?

Thanks

jmiller

unread,
Apr 4, 2009, 4:14:26 PM4/4/09
to jaikuengine-discuss
Zealots code was a good start. Unfortunately oauth_callback in web
mode is broken. I submitted a patch and it looks like it's been
picked up. Hopefully it will be fixed soon.

http://code.google.com/p/jaikuengine/issues/detail?id=77
Reply all
Reply to author
Forward
0 new messages