Sunday 15 June 2014

Spring Social Reddit Extension - OAuth 2 Access_token retrieval. 401 Error -



Spring Social Reddit Extension - OAuth 2 Access_token retrieval. 401 Error -

i trying create extension reddit's api. reddit follows oauth 2 obtaining access_token. using springs resttemplate create post requests reddit. able finish first stage according documentation. user redirected reddit he/she allows application, reddit redirects me application code. however, sec stage doesn't seem work. must utilize code create post request :

https://ssl.reddit.com/api/v1/access_token

here effort obtaining accessgrant (springsocial wrapper accesstoken sent reddit). spring social requires extend oauth2template , implement authentication process there. in typical spring application, controller utilize helper create phone call redditoauth2template.exchangeforaccess , save returned accessgrant database.

according reddit api documentaiton 401 response occurs due lack of client credentials via http basic auth. however, doing in createheaders(string username, string password) method.

public class redditoauth2template extends oauth2template {

private static final logger log = logmanager.getlogger(redditoauth2template.class); private string client_id; private string client_secret; public redditoauth2template(string clientid, string clientsecret) { super(clientid, clientsecret, redditpaths.oauth_auth_url, redditpaths.oauth_token_url); this.client_id = clientid; this.client_secret = clientsecret; setuseparametersforclientauthentication(true); } @override @suppresswarnings({"unchecked", "rawtypes"}) protected accessgrant postforaccessgrant(string accesstokenurl, multivaluemap<string, string> parameters) { httpheaders headers = createheaders(client_id, client_secret); headers.setcontenttype(mediatype.application_form_urlencoded); headers.set(accesstokenurl, accesstokenurl); httpentity<multivaluemap<string, string>> requestentity = new httpentity<multivaluemap<string, string>>(parameters, headers); responseentity<map> responseentity = getresttemplate().exchange(accesstokenurl, httpmethod.post, requestentity, map.class); map<string, object> responsemap = responseentity.getbody(); homecoming extractaccessgrant(responsemap); } /* reddit requires client_id , client_secret placed via http basic auth when retrieving access_token */ private httpheaders createheaders(string username, string password) { string auth = username + ":" + password; byte[] encodedauth = base64.getencoder().encode(auth.getbytes(charset.forname("us-ascii"))); httpheaders headers = new httpheaders(); string authheader = "basic " + new string(encodedauth); headers.set("authorization", authheader); homecoming headers; } private accessgrant extractaccessgrant(map<string, object> result) { string accesstoken = (string) result.get("access_token"); string scope = (string) result.get("scope"); string refreshtoken = (string) result.get("refresh_token"); // result.get("expires_in") may integer, cast number first. number expiresinnumber = (number) result.get("expires_in"); long expiresin = (expiresinnumber == null) ? null : expiresinnumber.longvalue(); homecoming createaccessgrant(accesstoken, scope, refreshtoken, expiresin, result); }

}

if you're getting 401 response endpoint, you're doing 1 of little number of things wrong, related sending client id & secret http basic authorization:

not including formatted authorization header (i.e., authorization: basic <b64 encoded credentials>) not base of operations 64 encoding credentials not including client_id valid oauth2 client not including semicolon between client id , secret not including secret, or including wrong secret

you should check each stage of basic client auth, , log output (or utilize debugger inspect it) @ each stage ensure you're not missing anything. should inspect actual http request generate, , verify header beingness sent (some http libraries take liberties headers)

spring oauth-2.0 spring-social resttemplate reddit

No comments:

Post a Comment