尝试检索access_token时出现fileNotFoundException:
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.244c14ba79e842868a695192835c83ac01-01 11:50:39.371: W/System.err(21868): at libcore.net.http.httpURLConnectionImpl.getinputStream(httpURLConnectionImpl.java:186)
错误发生在这一行
JsONObject JsonObj = (JsONObject) new JsONTokener(streamToString(urlConnection.getinputStream())).nextValue();
我究竟做错了什么?
解决方法:
只需将InstagramApp.java替换为您当前在库中的类.
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, as 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 one rror(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 = 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 = 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 = 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 = 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 to get access token"); } else if(msg.arg1 == 2) { mListener.onFail("Failed to get 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(); } finally { 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); }}
总结 以上是内存溢出为你收集整理的android-尝试获取Instagram API的访问令牌时出现FileNotFoundException全部内容,希望文章能够帮你解决android-尝试获取Instagram API的访问令牌时出现FileNotFoundException所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)