在應用程序中我的要求是獲取用戶的照片然後顯示在我的程序中。
我進行了身份驗證,獲的訪問權限,輸入用戶名和密碼,但是還是顯示不允許我打開這個頁面。請問我如何獲得用戶的配置文件和照片呢?
關於這個問題,我使用的以下代碼:
String url ="https://instagram.com/oauth/authorize?" +"response_type=token" +
"&redirect_uri=" + CALLBACK_URL+"&scope=basic"+"&client_id=" + CLIENT_ID ;
WebView webview = (WebView)findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
String fragment = "#access_token=";
int start = url.indexOf(fragment);
if (start > -1) {
// You can use the accessToken for api calls now.
String accessToken = url.substring(start + fragment.length(), url.length());
Log.v(TAG, "OAuth complete, token: [" + accessToken + "].");
Log.i(TAG, "" +accessToken);
Toast.makeText(ActivityWebView.this, "Token: " + accessToken, Toast.LENGTH_SHORT).show();
}
}
});
webview.loadUrl(url);
}
我是用以下代碼解決的:
URL example = new URL("https://api.instagram.com/v1/users/self/media/recent?access_token="
+ accessToken);
URLConnection tc = example.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
JSONObject ob = new JSONObject(line);
JSONArray object = ob.getJSONArray("data");
for (int i = 0; i < object.length(); i++) {
JSONObject jo = (JSONObject) object.get(i);
JSONObject nja = (JSONObject) jo.getJSONObject(photos);
JSONObject purl3 = (JSONObject) nja
.getJSONObject("thumbnail");
PhotoSet set = new PhotoSet();
set.setThumb(purl3.getString("url"));
thesets.add(set);
Log.i(TAG, "" + purl3.getString("url"));
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}