4

在我的应用程序中,我的要求是获取用户照片并在我的应用程序中显示。

为此,我进行了身份验证,获得了访问令牌并输入了用户名密码,但这表明您无权打开此页面。接下来如何获取用户资料和照片。

这里我的代码是:

 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);
}
4

2 回答 2

10

试试这个代码我得到了解决方案:

 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");
                    Log.i(TAG, "" + purl3.getString("url"));
                }

            }
        } catch (MalformedURLException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } catch (JSONException e) {

            e.printStackTrace();
        }

    }
于 2012-09-24T11:06:35.850 回答
2

得到/users/[USER_ID]/media/recent/?access_token=[ACCESS_TOKEN]

您可以点击此 URL 以获取用户的供稿(最后 20 张照片)。这将为您提供一个 JSON 数组,然后您需要在应用程序中根据需要对其进行解析和显示。

于 2012-09-20T04:20:45.023 回答