在Android中获取用户生日Instagram

时间:2015-10-08 07:00:37

标签: android instagram

我按照本教程整合了Instagram:

http://www.theappguruz.com/blog/instagram-integration-android-application-tutorial

现在我需要获得用户生日,但我不知道如何做到这一点。

这是我的代码:

  

InstagramDialog

public class InstagramDialog extends Dialog {
    private CookieManager cookieManager;
    private String mUrl;
    private OAuthDialogListener mListener;
    private ProgressDialog mSpinner;
    private WebView mWebView;
    private Context mContext;
    public InstagramDialog(Context context, String url, OAuthDialogListener listener) {
        super(context, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        this.mContext = context;
        mUrl = url;
        mListener = listener;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_instagram_dialog);
        mSpinner = new ProgressDialog(getContext());
        mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mSpinner.setMessage("Loading...");
        setUpWebView();
    }
    private void setUpWebView() {
        mWebView = (WebView) findViewById(R.id.instagramWebView);
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setWebViewClient(new OAuthWebViewClient());
        mWebView.loadUrl(mUrl);
    }
    public void clearCookie() {
        cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        cookieManager.removeAllCookie();
    }

    private class OAuthWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.d(TAG, "Redirecting URL " + url);

            if (url.startsWith(InstagramApplication.mCallbackUrl)) {
                String urls[] = url.split("=");
                mListener.onComplete(urls[1]);
                InstagramDialog.this.dismiss();

                return true;
            }
            return false;
        }
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Log.d(TAG, "Page error: " + description);

            super.onReceivedError(view, errorCode, description, failingUrl);
            mListener.onError(description);
            InstagramDialog.this.dismiss();
        }
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            Log.d(TAG, "Loading URL: " + url);

            super.onPageStarted(view, url, favicon);
            mSpinner.show();
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Log.d(TAG, "onPageFinished URL: " + url);
            mSpinner.dismiss();
        }
    }
    public interface OAuthDialogListener {
        public abstract void onComplete(String accessToken);

        public abstract void onError(String error);
    }
}
  

InstagramApplication

public class InstagramApplication {
    private InstagramDialog mDialog;
    private OAuthAuthenticationListener mListener;
    private ProgressDialog mProgress;
    private String mAuthUrl;
    private String mAccessToken;
    private Context mCtx;
    private String mClientId;
    private String mClientSecret;
    public static int WHAT_ERROR = 1;
    private static int WHAT_FETCH_INFO = 2;
    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";
    public InstagramApplication(Context context, String clientId, String clientSecret, String callbackUrl) {
        mClientId = clientId;
        mClientSecret = clientSecret;
        mCtx = context;
        mAccessToken = AppHelper.getString(mCtx, APIDefinition.ACCESS_TOKEN);
        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";
        InstagramDialog.OAuthDialogListener listener = new InstagramDialog.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.v(TAG, "Getting access token");
                int what = WHAT_FETCH_INFO;
                try {
                    URL url = new URL(TOKEN_URL);
                    Log.i(TAG, "Opening Token URL " + url.toString());
                    HttpURLConnection urlConnection = (HttpURLConnection) url
                            .openConnection();
                    urlConnection.setRequestMethod("POST");
                    urlConnection.setDoInput(true);
                    urlConnection.setDoOutput(true);
                    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());
//handle get info
catch (Exception ex) {
                    what = WHAT_ERROR;
                    ex.printStackTrace();
                }
                mHandler.sendMessage(mHandler.obtainMessage(what, 1, 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) {
                mProgress.dismiss();
                mListener.onSuccess();
            }
        }
    };
    public void setListener(OAuthAuthenticationListener listener) {
        mListener = listener;
    }
    public void authorize() {
        mDialog.show();
    }

    public void resetAccessTokenClearCookie() {
        if (mAccessToken != null) {
            mDialog.clearCookie();
            mAccessToken = null;
        }
    }
    public static 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 interface OAuthAuthenticationListener {
        public abstract void onSuccess();

        public abstract void onFail(String error);
    }
}
  

主要

instagramApp = new InstagramApplication(this, getString(R.string.instagram_app_id),getString(R.string.instagram_key_secret),getString(R.string.instagram_callback_url));

instagramApp.setListener(new InstagramApplication.OAuthAuthenticationListener() {

    @Override
    public void onSuccess() {
        listener.onHandleLoginCallback(true);
    }

    @Override
    public void onFail(String error) {
    }
});
ImageButton loginButon = (ImageButton) findViewById(R.id.instargram_button);
loginButon.setOnClickListener(this);

@Override
public void onClick(View v) {
    if (v.getId() == R.id.instargram_button) {
        instagramApp.authorize();
    }
}

我的代码运行正常,但我不知道如何让用户过生日。

1 个答案:

答案 0 :(得分:0)

Instagram的用户无法设置他们的生日。 Instagram中的Birthday隐藏字段供将来使用,可能用于广告。

有些用户在usernamefullnameabout字段中写下他们的生日。您可以解析某些日期或字段(如y.o.

)的字段