Esempio n. 1
0
  private void startAuth() {
    final Uri callbackUrl = getCallbackUrl();
    final Twitter twitter = getTwitter(this);
    Configuration conf = twitter.getConfiguration();
    if (conf.getOAuthAccessToken() != null && conf.getOAuthAccessTokenSecret() != null) {
      Toast.makeText(getApplicationContext(), "認証済みです。", Toast.LENGTH_LONG).show();
      return;
    }

    setProgressBarIndeterminateVisibility(true);

    // 通信開始
    new AsyncTask<Void, Void, RequestToken>() {
      @Override
      protected RequestToken doInBackground(Void... params) {
        RequestToken requestToken = null;
        try {
          requestToken = twitter.getOAuthRequestToken(callbackUrl.toString());
        } catch (TwitterException e) {
          Log.e(TAG, "認証失敗", e);
        }
        return requestToken;
      }

      @Override
      protected void onPostExecute(RequestToken requestToken) {
        super.onPostExecute(requestToken);

        setProgressBarIndeterminateVisibility(false);

        mRequestToken = requestToken;
        if (requestToken != null) {
          String authorizationUrl = requestToken.getAuthorizationURL();
          Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(authorizationUrl));
          startActivity(intent);
        } else {
          Toast.makeText(getApplicationContext(), "Request Tokenが取得できませんでした。", Toast.LENGTH_LONG)
              .show();
        }
      }
    }.execute();
  }