OAuth Refresh Trouble

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

<

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

Hi Ben,

You’re a genius!! Thanks for the pointer. Just added the following to the
bottom of the request:

response = buf.getvalue()
print (response)

This prints the token, type and expiry as required. I’ve been pulling my
hair out with this one. Was staring me in the face this whole time.

Thanks very, very much for the help.
KarlOn Tuesday, November 5, 2013 9:25:11 PM UTC, Ben wrote:

Karl,

Presumably the body is written to buf, but I don’t see any code where
you read the contents of buf. Can you print out the contents after the
call to getUserData?

Ben

On Sunday, 3 November 2013 12:49:41 UTC-8, Karl Sayle wrote:

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 incertificate 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

<

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

Karl,

I’m glad that worked!

Best wishes,
BenOn Wed, Nov 6, 2013 at 3:28 AM, Karl Sayle karlsayle@gmail.com wrote:

Hi Ben,

You’re a genius!! Thanks for the pointer. Just added the following to the
bottom of the request:

response = buf.getvalue()
print (response)

This prints the token, type and expiry as required. I’ve been pulling my
hair out with this one. Was staring me in the face this whole time.

Thanks very, very much for the help.
Karl

On Tuesday, November 5, 2013 9:25:11 PM UTC, Ben wrote:

Karl,

Presumably the body is written to buf, but I don’t see any code where
you read the contents of buf. Can you print out the contents after the
call to getUserData?

Ben

On Sunday, 3 November 2013 12:49:41 UTC-8, Karl Sayle wrote:

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 incertificate 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

<

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


You received this message because you are subscribed to the Google Groups
“FreeAgent API” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to freeagent_api+unsubscribe@googlegroups.com.
To post to this group, send email to freeagent_api@googlegroups.com.
Visit this group at http://groups.google.com/group/freeagent_api.
For more options, visit https://groups.google.com/groups/opt_out.

Ben Brinckerhoff
Senior Engineer
FreeAgent

Web. freeagent.com http://www.freeagent.com/
Blog. freeagent.com/blog
Twitter. @freeagent https://twitter.com/#!/freeagent
Facebook. facebook.com/freeagentapp

40 Torphichen Street, Edinburgh, EH3 8JB
FreeAgent Central Ltd. Registered in sunny Scotland SC316774

Karl,

Presumably the body is written to buf, but I don’t see any code where you
read the contents of buf. Can you print out the contents after the call
to getUserData?

BenOn Sunday, 3 November 2013 12:49:41 UTC-8, Karl Sayle wrote:

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 incertificate 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

<

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