curl - OAuth2 Authentication in Java using HttpClient -
i'm attempting connect freesound api using java , apache http client (version 4.3).
i have succeeded in getting application handle authentication process. is, have exchanged user's authorization code access token , refresh token, outlined in step 3 of freesound api oauth2 authentication procedure.
now wish download sounds api.
the documentation downloading sounds api gives illustration curl command trying mimic in java.
curl -h "authorization: bearer {{access_token}}" 'https://www.freesound.org/apiv2/sounds/14854/download/'
unfortunately, way know how download files urls apache commons io line:
org.apache.commons.io.fileutils.copyurltofile(url, file)
since new link not have file extension, nor allow me specify authorization header, cannot utilize method.
my code not work. please help!
private static void downloadsound() { // output stream save .wav file fileoutputstream out; // file save file f = new file("test.wav"); // initializes http client , builds authorization header httpresponse res; closeablehttpclient httpclient = httpclients.createdefault(); string authorizationstring = "bearer " + accesstoken; seek { // assigns url , authorization header request httpget request = new httpget( uri.create("https://www.freesound.org/apiv2/sounds/14854/download/")); request.addheader("authentication", authorizationstring); // sends request res = httpclient.execute(request); // downloads file fileoutputstream out = new fileoutputstream(f); int read = 0; byte[] bytes = new byte[1024]; while ((read = res.getentity().getcontent().read(bytes)) != -1) { out.write(bytes, 0, read); } system.out.println("done!"); // closes input/output streams res.getentity().getcontent().close(); out.close(); } grab (ioexception e) { e.printstacktrace(); } }
it might authorization vs authentication
curl -h "authorization: bearer {{access_token}}"
vs
request.addheader("authentication", authorizationstring);
you might have easier time using apache httpclient, designed computer computer communication.
java curl oauth-2.0 apache-httpclient-4.x apache-commons-io
No comments:
Post a Comment