Why am I getting a 404 error with my python script accessing Confluence via OAUTH?
Asked by Message Systems:

Remote API is enabled.  
I followed the instructions for setting up OAUTH with Confluence (following similar instructions for JIRA works).  
My script returns a 404 error.

I've bypassed my proxy and hit Tomcat directly and I get the same error. I'm not sure how to proceed.

Any help appreciated.

Script below:

```
#!/usr/bin/env python## You must set the next two variables belowconsumer_key = 'test-oauth'conf_page = '24645678'private_key = 'mykey.pem'conf_server = 'https://confluence.int.mycompany.com'## You shouldn't have to modify below this lineimport base64import oauth2 as oauthimport os.pathimport ConfigParserclass SignatureMethod_RSA_SHA1(oauth.SignatureMethod):    name = 'RSA-SHA1'
    def signing_base(self, request, consumer, token):        if not hasattr(request, 'normalized_url') or request.normalized_url is None:            raise ValueError("Base URL for request is not set.")
        sig = (            oauth.escape(request.method),            oauth.escape(request.normalized_url),            oauth.escape(request.get_normalized_parameters()),        )
        key = '%s&' % oauth.escape(consumer.secret)        if token:            key += oauth.escape(token.secret)        raw = '&'.join(sig)        return key, raw
    def sign(self, request, consumer, token):        """Builds the base signature string."""        key, raw = self.signing_base(request, consumer, token)
        if not os.path.isfile(private_key):            print "Error: Private key file '" + private_key + "' not found."            quit()        with open(private_key, 'r') as f:            data = f.read()        privateKeyString = data.strip()
        from tlslite.utils import keyfactory        privatekey = keyfactory.parsePrivateKey(privateKeyString)        signature = privatekey.hashAndSign(raw)
        return base64.b64encode(signature)
rcfile = 'conf_config.txt'
def read_config():    data = {}    if not os.path.isfile(rcfile):        return False    else:        Config = ConfigParser.ConfigParser()        Config.read(rcfile)        data['token'] = Config.get('auth', 'oauth_token')        data['secret'] = Config.get('auth', 'oauth_token_secret')        return data
oauth_token = read_config()
consumer_secret = 'dont_care'
request_token_url = conf_server + '/plugins/servlet/oauth/request-token'access_token_url = conf_server + '/plugins/servlet/oauth/access-token'authorize_url = conf_server + '/plugins/servlet/oauth/authorize'
data_url = conf_server + '/rest/api/content/' + conf_page
consumer = oauth.Consumer(consumer_key, consumer_secret)if oauth_token == False:    client = oauth.Client(consumer)else:    accessToken = oauth.Token(oauth_token['token'], oauth_token['secret'])    client = oauth.Client(consumer, accessToken)    client.set_signature_method(SignatureMethod_RSA_SHA1())
    resp, content = client.request(data_url, "GET")    if resp['status'] != '200':        raise Exception('Something went wrong: ' + str(resp))    else:        print 'Success!'    quit()
print data_urlresp, content = client.request(data_url, "GET")if resp['status'] != '401':    raise Exception('Something went wrong, you should have no access but do! ' + str(resp))
consumer = oauth.Consumer(consumer_key, consumer_secret)client = oauth.Client(consumer)client.set_signature_method(SignatureMethod_RSA_SHA1())
resp, content = client.request(request_token_url, "POST")if resp['status'] != '200':    raise Exception("Invalid response %s: %s" % (resp['status'], content))
import urlparserequest_token = dict(urlparse.parse_qsl(content))
print "Request Token:"print " - oauth_token = %s" % request_token['oauth_token']print " - oauth_token_secret = %s" % request_token['oauth_token_secret']print
print "Go to the following link in your browser:"print "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token'])print
accepted = 'n'while accepted.lower() == 'n':    accepted = raw_input('Have you authorized me? (y/n) ')
token = oauth.Token(request_token['oauth_token'],    request_token['oauth_token_secret'])client = oauth.Client(consumer, token)client.set_signature_method(SignatureMethod_RSA_SHA1())
resp, content = client.request(access_token_url, "POST")access_token = dict(urlparse.parse_qsl(content))
print "Access Token:"print " - oauth_token = %s" % access_token['oauth_token']print " - oauth_token_secret = %s" % access_token['oauth_token_secret']printprint "You may now access protected resources using the access tokens above."print
outf = open(rcfile, 'w')outf.write("[auth]\n")outf.write("oauth_token=" + access_token['oauth_token'] + "\n")outf.write("oauth_token_secret=" + access_token['oauth_token_secret'] + "\n")outf.close()print "Access Token information saved to " + rcfile
accessToken = oauth.Token(access_token['oauth_token'], access_token['oauth_token_secret'])client = oauth.Client(consumer, accessToken)client.set_signature_method(SignatureMethod_RSA_SHA1())
resp, content = client.request(data_url, "GET")if resp['status'] != '200':    raise Exception("Something went wrong; you should have access, but dont! " + str(resp))
```


Answers:
author: Gonchik Tsymzhitov
body: Hi!

Have you added Oauth token?


Tags: oauth

python