Hi all,
I’ve been working on this script for a while now. The script works fine but
still requires me to manually acquire an access token from OAuth
Playground. As i want to automate the script i want it to obtain a new
token each time using the refresh token.
As far as a i can tell the script below should work (im going off the
Freeagent docs) but as you see from the response the token is missing.
REQUEST:
from time import gmtime, strftime
import pycurl
import urllib
import urllib2
from urllib2 import urlopen
import urlparse
import json
from json import JSONEncoder
import re
import oauthlib as oauth
import io
from io import BytesIO
import datetime
import smtplib
buf = io.BytesIO()
data = {‘client_secret’ : ‘%s’ % str(‘xxxxxxxxxxxxxxxxxxx’),
‘grant_type’ : ‘refresh_token’,
‘refresh_token’ : ‘%s’ % str(‘xxxxxxxxxxxxxxxxxxx’),
‘client_id’ : ‘%s’ % str(‘xxxxxxxxxxxxxxxxxxx’)}
post = urllib.urlencode(data)
def getUserData():
c = pycurl.Curl()
c.setopt(pycurl.URL, “https://api.freeagent.com/v2/token_endpoint”)
c.setopt(pycurl.HTTPHEADER, [
‘Accept: application/json’,
‘Content-Type: application/x-www-form-urlencoded’
])
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, post)
c.setopt(pycurl.WRITEFUNCTION, buf.write)
c.setopt(pycurl.VERBOSE, True)
c.setopt(c.SSL_VERIFYPEER, 0)
c.perform()
getUserData()
Please ignore the various imports, they refer to other parts of the script.
REPONSE:
- timeout on name lookup is not supported
- About to connect() to api.freeagent.com port 443 (#0)
- Trying 109.73.126.94…
- connected
- Connected to api.freeagent.com (109.73.126.94) port 443 (#0)
- libcurl is now using a weak random seed!
- SSL connection using ECDHE-RSA-RC4-SHA
- Server certificate:
- subject: OU=Domain Control Validated; OU=Hosted by XILO Communications
Ltd.; OU=EssentialSSL Wildcard; CN=*.freeagent.com - start date: 2013-03-05 00:00:00 GMT
- expire date: 2015-03-05 23:59:59 GMT
- subjectAltName: api.freeagent.com matched
- issuer: C=GB; ST=Greater Manchester; L=Salford; O=COMODO CA Limited; CN
=EssentialSSL CA - SSL certificate verify result: self signed certificate in certificate
chain (19), continuing anyway.
POST /v2/token_endpoint HTTP/1.1
User-Agent: PycURL/7.28.1
Host: api.freeagent.com
Accept: application/json
Content-Type: application/x-www-form-urlencoded
Content-Length: 150
- upload completely sent off: 150 out of 150 bytes
< HTTP/1.1 200 OK
< Server: nginx/1.4.1
< Date: Thu, 31 Oct 2013 14:17:06 GMT
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Status: 200 OK
< Cache-Control: no-store
< Pragma: no-cache
< X-UA-Compatible: IE=Edge,chrome=1
< ETag: “xxxxxxxxxxxxxxxxxxx”
< X-Request-Id: xxxxxxxxxxxxxxxxxxx
< X-Runtime: 0.127130
< X-Rev: 8459f5e
< X-Host: web4-gc
<
- Connection #0 to host api.freeagent.com left intact
- Closing connection #0
From what i can tell this response is correct bar the missing json content
containing the returned access token amongst other items.
If any one can help with this final part of the script id be very grateful
and if you need any further details ive missed please let me know.
Thanks,
Karl