android - FileNotFoundException when trying to get acess token of Instagram API -
i filenotfoundexception
while trying retrieve access_token:
java.io.filenotfoundexception: https://api.instagram.com/oauth/access_token&client_id=e909da82f8544a70bb9b29434xxxxxx&client_secret=fa34037e0f534628bb9becd1a3xxxxxx&grant_type=authorization_code&redirect_uri=x-oauthflow-instagram://callback&code=520401255.e909da8.244c14ba79e842868a695192835c83ac 01-01 11:50:39.371: w/system.err(21868): @ libcore.net.http.httpurlconnectionimpl.getinputstream(httpurlconnectionimpl.java:186)
the error occurring @ line
jsonobject jsonobj = (jsonobject) new jsontokener(streamtostring(urlconnection.getinputstream())).nextvalue();
what doing wrong?
just replace instagramapp.java current class in library.
package br.com.dina.oauth.instagram; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstreamwriter; import java.net.httpurlconnection; import java.net.url; import org.json.jsonobject; import org.json.jsontokener; import android.app.progressdialog; import android.content.context; import android.content.intent; import android.net.uri; import android.os.handler; import android.os.message; import android.util.log; import br.com.dina.oauth.instagram.instagramdialog.oauthdialoglistener; /** * * @author thiago locatelli <thiago.locatelli@gmail.com> * @author lorensius w. l t <lorenz@londatiga.net> * */ public class instagramapp { private instagramsession msession; private instagramdialog mdialog; private oauthauthenticationlistener mlistener; private progressdialog mprogress; private string mauthurl; private string mtokenurl; private string maccesstoken; private context mctx; private string mclientid; private string mclientsecret; private static int what_finalize = 0; private static int what_error = 1; private static int what_fetch_info = 2; /** * callback url, set in 'manage oauth costumers' page * (https://developer.github.com/) */ public static string mcallbackurl = ""; private static final string auth_url = "https://api.instagram.com/oauth/authorize/"; private static final string token_url = "https://api.instagram.com/oauth/access_token"; private static final string api_url = "https://api.instagram.com/v1"; private static final string tag = "instagramapi"; public instagramapp(context context, string clientid, string clientsecret, string callbackurl) { mclientid = clientid; mclientsecret = clientsecret; mctx = context; msession = new instagramsession(context); maccesstoken = msession.getaccesstoken(); mcallbackurl = callbackurl; mtokenurl = token_url + "?client_id=" + clientid + "&client_secret=" + clientsecret + "&redirect_uri=" + mcallbackurl + "&grant_type=authorization_code"; mauthurl = auth_url + "?client_id=" + clientid + "&redirect_uri=" + mcallbackurl + "&response_type=code&display=touch&scope=likes+comments+relationships"; oauthdialoglistener listener = new oauthdialoglistener() { @override public void oncomplete(string code) { getaccesstoken(code); } @override public void onerror(string error) { mlistener.onfail("authorization failed"); } }; mdialog = new instagramdialog(context, mauthurl, listener); mprogress = new progressdialog(context); mprogress.setcancelable(false); } private void getaccesstoken(final string code) { mprogress.setmessage("getting access token ..."); mprogress.show(); new thread() { @override public void run() { log.i(tag, "getting access token"); int = what_fetch_info; try { url url = new url(token_url); //url url = new url(mtokenurl + "&code=" + code); log.i(tag, "opening token url " + url.tostring()); httpurlconnection urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setrequestmethod("post"); urlconnection.setdoinput(true); urlconnection.setdooutput(true); //urlconnection.connect(); outputstreamwriter writer = new outputstreamwriter(urlconnection.getoutputstream()); writer.write("client_id="+mclientid+ "&client_secret="+mclientsecret+ "&grant_type=authorization_code" + "&redirect_uri="+mcallbackurl+ "&code=" + code); writer.flush(); string response = streamtostring(urlconnection.getinputstream()); log.i(tag, "response " + response); jsonobject jsonobj = (jsonobject) new jsontokener(response).nextvalue(); maccesstoken = jsonobj.getstring("access_token"); log.i(tag, "got access token: " + maccesstoken); string id = jsonobj.getjsonobject("user").getstring("id"); string user = jsonobj.getjsonobject("user").getstring("username"); string name = jsonobj.getjsonobject("user").getstring("full_name"); msession.storeaccesstoken(maccesstoken, id, user, name); } catch (exception ex) { = what_error; ex.printstacktrace(); } mhandler.sendmessage(mhandler.obtainmessage(what, 1, 0)); } }.start(); } private void fetchusername() { mprogress.setmessage("finalizing ..."); new thread() { @override public void run() { log.i(tag, "fetching user info"); int = what_finalize; try { url url = new url(api_url + "/users/" + msession.getid() + "/?access_token=" + maccesstoken); log.d(tag, "opening url " + url.tostring()); httpurlconnection urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setrequestmethod("get"); urlconnection.setdoinput(true); //urlconnection.setdooutput(true); urlconnection.connect(); string response = streamtostring(urlconnection.getinputstream()); system.out.println(response); jsonobject jsonobj = (jsonobject) new jsontokener(response).nextvalue(); string name = jsonobj.getjsonobject("data").getstring("full_name"); string bio = jsonobj.getjsonobject("data").getstring("bio"); log.i(tag, "got name: " + name + ", bio [" + bio + "]"); } catch (exception ex) { = what_error; ex.printstacktrace(); } mhandler.sendmessage(mhandler.obtainmessage(what, 2, 0)); } }.start(); } private handler mhandler = new handler() { @override public void handlemessage(message msg) { if (msg.what == what_error) { mprogress.dismiss(); if(msg.arg1 == 1) { mlistener.onfail("failed access token"); } else if(msg.arg1 == 2) { mlistener.onfail("failed user information"); } } else if(msg.what == what_fetch_info) { fetchusername(); } else { mprogress.dismiss(); mlistener.onsuccess(); } } }; public boolean hasaccesstoken() { return (maccesstoken == null) ? false : true; } public void setlistener(oauthauthenticationlistener listener) { mlistener = listener; } public string getusername() { return msession.getusername(); } public string getid() { return msession.getid(); } public string getname() { return msession.getname(); } public void authorize() { //intent webauthintent = new intent(intent.action_view); //webauthintent.setdata(uri.parse(auth_url)); //mctx.startactivity(webauthintent); mdialog.show(); } private string streamtostring(inputstream is) throws ioexception { string str = ""; if (is != null) { stringbuilder sb = new stringbuilder(); string line; try { bufferedreader reader = new bufferedreader( new inputstreamreader(is)); while ((line = reader.readline()) != null) { sb.append(line); } reader.close(); } { is.close(); } str = sb.tostring(); } return str; } public void resetaccesstoken() { if (maccesstoken != null) { msession.resetaccesstoken(); maccesstoken = null; } } public interface oauthauthenticationlistener { public abstract void onsuccess(); public abstract void onfail(string error); } }
Comments
Post a Comment