java


Trying to get an Access Token from Dwolla restful api


Im having trouble getting an access token for the sandbox environment.
Im following this guide for authenticating: OAuth
So when i create my request, following this guide, i get the following response from the api:
{"error":"access_denied","error_description":"Invalid application credentials."}
Im using key for mf client ID and secret as my client secret as per the instructions.
Here is the code Im using:
public static void main(String[] args) {
try {
URL url = new URL("https://www.dwolla.com/oauth/v2/token");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("client_id", "<Key>");
conn.setRequestProperty("client_secret", "<Secret>");
conn.setRequestProperty("grant_type", "client_credentials");
conn.setDoInput(true);
conn.setDoOutput(true);
System.out.println("Message:" + conn.getResponseMessage());
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException ex) {
Logger.getLogger(PaymentTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(PaymentTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
I was able to get an access token finally. My problem is first of all the above code uses client_id and client_secret as header params. These need to go in the body of the request.
My second problem is that I used the wrong content type for the message I was sending.
Here is the code that worked for me:
URL url = new URL("https://sandbox.dwolla.com/oauth/v2/token");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoInput(true);
conn.setDoOutput(true);
String data = "";
JSONObject jsonObj = new JSONObject();
jsonObj.put("client_id", "<Your Client ID>");
jsonObj.put("client_secret", "<Your Client Secret>");
jsonObj.put("grant_type", "client_credentials");
data = jsonObj.toString();
System.out.println("data = " + data);
byte[] outputInBytes = data.getBytes("UTF-8");
OutputStream os = conn.getOutputStream();
os.write( outputInBytes );
os.close();
System.out.println("Message:" + conn.getResponseMessage());
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();

Related Links

JSon Serialization of a final class into Redis using Jackson ObjectMapper and spring-session
Servlet didn't make the connection with JDBC? [duplicate]
JAVA - Calendar.getInstance().get(Calendar.YEAR) VS Calender.YEAR
Gradle: multi project with different source & target compatibility
Difference between replace and put for HashMap
converting csv to xls in java taking care of a column
Java rectangle cannot find symbol
this is very simple program of collection but not running [closed]
How to capture a hive exit status or error code using JDBC API
How to connect smack client to ejabberd Server with SSL
What does 'intrinsify' mean in the JVM source code?
Implicit intent issue in android
How does this parameter change inside the method
change toast message using switch
Can't commit JPA transaction - RollbackException: Transaction marked as rollbackOnly
could not read XML Values in Java

Categories

HOME
sendgrid
winforms
date
keycloak
isabelle
server
react-virtualized
relative-path
youtube-api-v3
cakephp-2.5
v8
mouse
bs4
multiple-records
adfs
facebook-messenger-bot
dacpac
rascal
modx-revolution
mapserver
jsprit
clojurescript
propel
solaris-10
text-rendering
alpine
tokenize
dcevm
autosys
su
intel-pin
neo4j-spatial
librato
functor
noraui
typed.js
rotational-matrices
mpmediaquery
bitbucket-pipelines
hue
eclipse-scout
import-from-excel
flashair
businessworks
long-polling
upstart
hendrix
winscp-net
darcs
directory-structure
lift-json
jquery-filter
firmata
login-control
asp.net-4.5
django-debug-toolbar
hill-climbing
sdf
hexdump
captivenetwork
kendonumerictextbox
google-places
citrus-pay
jqgrid-formatter
deis
operation
proj4js
streambase
lustre
bigbluebutton
website-monitoring
android-imagebutton
file-copying
javafx-webengine
xceed-datagrid
resty-gwt
cloud-connect
html5-notifications
uv-mapping
geos
cos
jquery-mobile-dialog
spring-validator
gil
qtkit
transactionscope
data-loss
hamachi
yui-datatable
celltable
pyinotify
datareader
yslow
microsoft-virtualization
data-driven
anti-piracy
putchar
mdac
ugc

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile