如何在Android中集成Yahoo

时间:2015-03-27 08:53:10

标签: android

我是android开发的新手。我正在开发Android应用程序中的雅虎登录集成并返回主Activity。我已经完成了yahoo集成的所有步骤。我花了几天时间在android中搜索yahoo集成但是我找不到正确的方法。当我运行这个应用程序时,Toast将出现包含消息: -

(E /与服务提供商的通信失败:null(4194):oauth.signpost.exception.OAuthCommunicationException:与服务提供商的通信失败:null)

然后会出现带OAuth引脚的对话框....请帮助我从哪里获得OAuth引脚以及如何通过此集成检索雅虎邮件。

package com.example.yhoointegration;



public class MainActivity extends ActionBarActivity {

    private static final String REQUEST_TOKEN_ENDPOINT_URL = "https://api.login.yahoo.com/oauth/v2/get_request_token";
    private static final String ACCESS_TOKEN_ENDPOINT_URL = "https://api.login.yahoo.com/oauth/v2/get_access_token";
    private static final String AUTHORIZE_WEBSITE_URL = "https://api.login.yahoo.com/oauth/v2/request_auth";
    private static final int PIN_DIALOG = 0;
    String CALLBACK_URL = ""; // this should be the same as
                                                // the
    // SCHEME and HOST values in
    // your AndroidManifes""t.xml file
    String CONSUMER_KEY = "";
    String CONSUMER_SECRET = "";
    private CommonsHttpOAuthConsumer myConsumer;
    private CommonsHttpOAuthProvider myProvider;
    private String requestToken;
    private String accessToken;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        callOAuth();
        showDialog(PIN_DIALOG);

    }

    private void callOAuth() {
        try {
            // retrieve the consumer token and then sign it
            myConsumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY,
                    CONSUMER_SECRET);

            myConsumer.setMessageSigner(new HmacSha1MessageSigner());

            HttpClient client = new DefaultHttpClient();
            // retrieve the provider by using the signed consumer token
            myProvider = new CommonsHttpOAuthProvider(
                    REQUEST_TOKEN_ENDPOINT_URL, ACCESS_TOKEN_ENDPOINT_URL,
                    AUTHORIZE_WEBSITE_URL, client);
            myProvider.setOAuth10a(true);
            String aUrl = myProvider.retrieveRequestToken(myConsumer,
                    CALLBACK_URL);

            requestToken = myConsumer.getToken();
            Log.e("Token///", "" + requestToken);
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(aUrl)));
        } catch (Exception ex) {
            Toast.makeText(getApplicationContext(), ex.getMessage(),
                    Toast.LENGTH_LONG).show();
            Log.e(ex.getMessage(), ex.toString());
        }
    }

    // this is the callback function that will run when oauth authenticates
    // successfully
    @Override
    protected void onNewIntent(Intent intent) {
        System.out.println("OnNewIntent...");
        Toast.makeText(getApplicationContext(), "OnNewIntent - It works!",
                Toast.LENGTH_LONG).show();
        // whatever you want to do after authenticating goes here ....
    }

    AlertDialog createPinDialog() {
        LayoutInflater factory = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

        // LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.pin, null);
        final EditText pinText = (EditText) textEntryView
                .findViewById(R.id.pin_text);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Twitter OAuth PIN");
        builder.setView(textEntryView);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                if (pinText != null)
                    gotOAuthPin(pinText.getText().toString());
                onResume();
            }
        });
        return builder.create();
    }

    private void gotOAuthPin(String pin) {
        SharedPreferences.Editor editor = getSharedPreferences("yahoo",
                MODE_PRIVATE).edit();
        try {
            myProvider.retrieveAccessToken(myConsumer, pin);
            accessToken = myConsumer.getToken();

        } catch (OAuthMessageSignerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (accessToken != null && accessToken.length() > 0) {
            Toast.makeText(this, "Authorized", Toast.LENGTH_SHORT).show();
            HttpPost request = new HttpPost(
                    "http://social.yahooapis.com/v1/user/profile?format=json");
            StringEntity body = null;
            /*
             * try { body = new StringEntity("city=hamburg&label=" +
             * URLEncoder.encode("Send via Signpost!", "UTF-8")); } catch
             * (UnsupportedEncodingException e1) { // TODO Auto-generated catch
             * block e1.printStackTrace(); }
             * body.setContentType("application/x-www-form-urlencoded");
             * request.setEntity(body);
             */

            try {
                myConsumer.sign(request);
            } catch (OAuthMessageSignerException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (OAuthExpectationFailedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (OAuthCommunicationException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            System.out.println("Sending update request to Fire Eagle...");

            HttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = null;
            try {
                response = httpClient.execute(request);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Toast.makeText(
                    this,
                    "Response: " + response.getStatusLine().getStatusCode()
                            + " " + response.getStatusLine().getReasonPhrase(),
                    Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Not Authorized", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case PIN_DIALOG:
            LayoutInflater factory = LayoutInflater.from(this);
            final View textEntryView = factory.inflate(R.layout.pin, null);
            final EditText pinText = (EditText) textEntryView
                    .findViewById(R.id.pin_text);
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("OAuth PIN");
            builder.setView(textEntryView);
            builder.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            if (pinText != null)
                                gotOAuthPin(pinText.getText().toString());
                        }
                    });
            return builder.create();
        }

        return super.onCreateDialog(id);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

0 个答案:

没有答案
相关问题