/*package*/
 static ResponseList<UserList> createUserListList(HttpResponse res, Configuration conf)
     throws TwitterException {
   try {
     if (conf.isJSONStoreEnabled()) {
       TwitterObjectFactory.clearThreadLocalMap();
     }
     JSONArray list = res.asJSONArray();
     int size = list.length();
     ResponseList<UserList> users = new ResponseListImpl<UserList>(size, res);
     for (int i = 0; i < size; i++) {
       JSONObject userListJson = list.getJSONObject(i);
       UserList userList = new UserListJSONImpl(userListJson);
       users.add(userList);
       if (conf.isJSONStoreEnabled()) {
         TwitterObjectFactory.registerJSONObject(userList, userListJson);
       }
     }
     if (conf.isJSONStoreEnabled()) {
       TwitterObjectFactory.registerJSONObject(users, list);
     }
     return users;
   } catch (JSONException jsone) {
     throw new TwitterException(jsone);
   }
 }
Beispiel #2
0
 /** {@inheritDoc} */
 @Override
 public AccessToken getOAuthAccessToken(final String screenName, final String password)
     throws TwitterException {
   try {
     final String url = conf.getOAuthAccessTokenURL();
     if (0 == url.indexOf("http://")) {
       // SSL is required
       // @see https://dev.twitter.com/docs/oauth/xauth
       // url = "https://" + url.substring(7);
     }
     final String sign_url = conf.getSigningOAuthAccessTokenURL();
     if (0 == sign_url.indexOf("http://")) {
       // SSL is required
       // @see https://dev.twitter.com/docs/oauth/xauth
       // sign_url = "https://" + sign_url.substring(7);
     }
     oauthToken =
         new AccessToken(
             http.post(
                 url,
                 sign_url,
                 new HttpParameter[] {
                   new HttpParameter("x_auth_username", screenName),
                   new HttpParameter("x_auth_password", password),
                   new HttpParameter("x_auth_mode", "client_auth")
                 },
                 this));
     return (AccessToken) oauthToken;
   } catch (final TwitterException te) {
     throw new TwitterException(
         "The screen name / password combination seems to be invalid.", te, te.getStatusCode());
   }
 }
 /*package*/
 static ResponseList<Relationship> createRelationshipList(HttpResponse res, Configuration conf)
     throws TwitterException {
   try {
     if (conf.isJSONStoreEnabled()) {
       DataObjectFactoryUtil.clearThreadLocalMap();
     }
     JSONArray list = res.asJSONArray();
     int size = list.length();
     ResponseList<Relationship> relationships = new ResponseListImpl<Relationship>(size, res);
     for (int i = 0; i < size; i++) {
       JSONObject json = list.getJSONObject(i);
       Relationship relationship = new RelationshipJSONImpl(json);
       if (conf.isJSONStoreEnabled()) {
         DataObjectFactoryUtil.registerJSONObject(relationship, json);
       }
       relationships.add(relationship);
     }
     if (conf.isJSONStoreEnabled()) {
       DataObjectFactoryUtil.registerJSONObject(relationships, list);
     }
     return relationships;
   } catch (JSONException jsone) {
     throw new TwitterException(jsone);
   } catch (TwitterException te) {
     throw te;
   }
 }
Beispiel #4
0
 /** {@inheritDoc} */
 @Override
 public RequestToken getOAuthRequestToken(final String callbackURL, final String xAuthAccessType)
     throws TwitterException {
   if (oauthToken instanceof AccessToken)
     throw new IllegalStateException("Access token already available.");
   final List<HttpParameter> params = new ArrayList<HttpParameter>();
   if (callbackURL != null) {
     params.add(new HttpParameter("oauth_callback", callbackURL));
   }
   if (xAuthAccessType != null) {
     params.add(new HttpParameter("x_auth_access_type", xAuthAccessType));
   }
   final String url = conf.getOAuthRequestTokenURL();
   if (0 == url.indexOf("http://")) {
     // SSL is required
     // @see https://dev.twitter.com/docs/oauth/xauth
     // url = "https://" + url.substring(7);
   }
   final String sign_url = conf.getSigningOAuthRequestTokenURL();
   if (0 == sign_url.indexOf("http://")) {
     // SSL is required
     // @see https://dev.twitter.com/docs/oauth/xauth
     // sign_url = "https://" + sign_url.substring(7);
   }
   oauthToken =
       new RequestToken(
           conf,
           http.post(url, sign_url, params.toArray(new HttpParameter[params.size()]), this),
           this);
   return (RequestToken) oauthToken;
 }
 /*package*/
 static ResponseList<Location> createLocationList(HttpResponse res, Configuration conf)
     throws TwitterException {
   if (conf.isJSONStoreEnabled()) {
     DataObjectFactoryUtil.clearThreadLocalMap();
   }
   return createLocationList(res.asJSONArray(), conf.isJSONStoreEnabled());
 }
Beispiel #6
0
 /** {@inheritDoc} */
 @Override
 public AccessToken getOAuthAccessToken() throws TwitterException {
   ensureTokenIsAvailable();
   if (oauthToken instanceof AccessToken) return (AccessToken) oauthToken;
   oauthToken =
       new AccessToken(
           http.post(conf.getOAuthAccessTokenURL(), conf.getSigningOAuthAccessTokenURL(), this));
   return (AccessToken) oauthToken;
 }
Beispiel #7
0
 /**
  * Returns a OAuth Authenticated instance.<br>
  * consumer key and consumer Secret must be provided by twitter4j.properties, or system
  * properties.<br>
  * Unlike {@link Twitter#setOAuthAccessToken(twitter4j.auth.AccessToken)}, this factory method
  * potentially returns a cached instance.
  *
  * @param accessToken access token
  * @return an instance
  * @since Twitter4J 2.1.9
  */
 public Twitter getInstance(AccessToken accessToken) {
   String consumerKey = conf.getOAuthConsumerKey();
   String consumerSecret = conf.getOAuthConsumerSecret();
   if (null == consumerKey && null == consumerSecret) {
     throw new IllegalStateException("Consumer key and Consumer secret not supplied.");
   }
   OAuthAuthorization oauth = new OAuthAuthorization(conf);
   oauth.setOAuthAccessToken(accessToken);
   return getInstance(oauth);
 }
 /*package*/ UserListJSONImpl(HttpResponse res, Configuration conf) throws TwitterException {
   super(res);
   if (conf.isJSONStoreEnabled()) {
     TwitterObjectFactory.clearThreadLocalMap();
   }
   JSONObject json = res.asJSONObject();
   init(json);
   if (conf.isJSONStoreEnabled()) {
     TwitterObjectFactory.registerJSONObject(this, json);
   }
 }
  /*package*/
  TwitterStreamImpl(Configuration conf, Authorization auth) {
    super(conf, auth);
    http = HttpClientFactory.getInstance(new StreamingReadTimeoutConfiguration(conf));
    // turning off keepalive connection explicitly because Streaming API doesn't need keepalive
    // connection.
    // and this will reduce the shutdown latency of streaming api connection
    // see also - http://jira.twitter4j.org/browse/TFJ-556
    http.addDefaultRequestHeader("Connection", "close");

    stallWarningsGetParam = "stall_warnings=" + (conf.isStallWarningsEnabled() ? "true" : "false");
    stallWarningsParam = new HttpParameter("stall_warnings", conf.isStallWarningsEnabled());
  }
 SavedSearchJSONImpl(HttpResponse httpresponse, Configuration configuration)
     throws TwitterException
 {
     super(httpresponse);
     if (configuration.isJSONStoreEnabled())
     {
         TwitterObjectFactory.clearThreadLocalMap();
     }
     httpresponse = httpresponse.asJSONObject();
     init(httpresponse);
     if (configuration.isJSONStoreEnabled())
     {
         TwitterObjectFactory.registerJSONObject(this, httpresponse);
     }
 }
 public int hashCode() {
   if (conf != null) {
     return conf.hashCode();
   } else {
     return 0;
   }
 }
 public String getAuthorizationURL() {
   return (new StringBuilder())
       .append(conf.getOAuthAuthorizationURL())
       .append("?oauth_token=")
       .append(getToken())
       .toString();
 }
 /*package*/ RelationshipJSONImpl(HttpResponse res, Configuration conf) throws TwitterException {
   this(res, res.asJSONObject());
   if (conf.isJSONStoreEnabled()) {
     DataObjectFactoryUtil.clearThreadLocalMap();
     DataObjectFactoryUtil.registerJSONObject(this, res.asJSONObject());
   }
 }
  public void testEmptyJSON() throws Exception {
    HttpClientImpl http = new HttpClientImpl();

    // empty User list
    List<User> users =
        UserJSONImpl.createUserList(
            http.get("http://twitter4j.org/en/testcases/statuses/friends/T4J_hudson.json"), conf);
    Assert.assertTrue(users.size() == 0);
    assertDeserializedFormIsEqual(users);

    // empty Status list
    List<Status> statuses =
        StatusJSONImpl.createStatusList(
            http.get("http://twitter4j.org/en/testcases/statuses/friends/T4J_hudson.json"), conf);
    Assert.assertTrue(statuses.size() == 0);
    assertDeserializedFormIsEqual(statuses);

    // empty DirectMessages list
    List<DirectMessage> directMessages =
        DirectMessageJSONImpl.createDirectMessageList(
            http.get("http://twitter4j.org/en/testcases/statuses/friends/T4J_hudson.json"), conf);
    Assert.assertTrue(directMessages.size() == 0);
    assertDeserializedFormIsEqual(directMessages);

    // empty Trends list
    List<Trends> trends =
        TrendsJSONImpl.createTrendsList(
            http.get("http://twitter4j.org/en/testcases/trends/daily-empty.json"),
            conf.isJSONStoreEnabled());
    Assert.assertTrue(trends.size() == 0);
    assertDeserializedFormIsEqual(trends);
  }
 /** {@inheritDoc} */
 public RequestToken getOAuthRequestToken(String callbackURL) throws TwitterException {
   HttpParameter[] params =
       null != callbackURL
           ? new HttpParameter[] {new HttpParameter("oauth_callback", callbackURL)}
           : new HttpParameter[0];
   oauthToken = new RequestToken(http.post(conf.getOAuthRequestTokenURL(), params, this), this);
   return (RequestToken) oauthToken;
 }
  private String upload() throws TwitterException {

    if (conf.getMediaProviderParameters() != null && this.appendParameter.length > 0) {
      this.postParameter = appendHttpParameters(this.postParameter, this.appendParameter);
    }
    String media_id_string_init = post();
    return media_id_string_init;
  }
  AbstractPhotoUploadImpl(Configuration conf, OAuthAuthorization oauth) {
    this.oauth = oauth;
    this.conf = conf;
    try {
    } catch (Exception e) {
      e.printStackTrace();
    }

    client = HttpClientFactory.getInstance(conf.getHttpClientConfiguration());
  }
 /** {@inheritDoc} */
 public AccessToken getOAuthAccessToken(String oauthVerifier) throws TwitterException {
   ensureTokenIsAvailable();
   oauthToken =
       new AccessToken(
           http.post(
               conf.getOAuthAccessTokenURL(),
               new HttpParameter[] {new HttpParameter("oauth_verifier", oauthVerifier)},
               this));
   return (AccessToken) oauthToken;
 }
    static ResponseList createDirectMessageList(HttpResponse httpresponse, Configuration configuration)
    {
        int i;
        JSONArray jsonarray;
        JSONObject jsonobject;
        DirectMessageJSONImpl directmessagejsonimpl;
        int j;
        try
        {
            if (configuration.isJSONStoreEnabled())
            {
                TwitterObjectFactory.clearThreadLocalMap();
            }
            jsonarray = httpresponse.asJSONArray();
            j = jsonarray.length();
            httpresponse = new ResponseListImpl(j, httpresponse);
        }
        // Misplaced declaration of an exception variable
        catch (HttpResponse httpresponse)
        {
            throw new TwitterException(httpresponse);
        }
        i = 0;
_L2:
        if (i >= j)
        {
            break MISSING_BLOCK_LABEL_89;
        }
        jsonobject = jsonarray.getJSONObject(i);
        directmessagejsonimpl = new DirectMessageJSONImpl(jsonobject);
        httpresponse.add(directmessagejsonimpl);
        if (configuration.isJSONStoreEnabled())
        {
            TwitterObjectFactory.registerJSONObject(directmessagejsonimpl, jsonobject);
        }
        break MISSING_BLOCK_LABEL_116;
        if (configuration.isJSONStoreEnabled())
        {
            TwitterObjectFactory.registerJSONObject(httpresponse, jsonarray);
        }
        return httpresponse;
        i++;
        if (true) goto _L2; else goto _L1
 @Override
 public Map<String, String> getRequestHeaders() {
   // turning off keepalive connection explicitly because Streaming API doesn't need keepalive
   // connection.
   // and this will reduce the shutdown latency of streaming api connection
   // see also - http://jira.twitter4j.org/browse/TFJ-556
   Map<String, String> headers = new HashMap<String, String>(nestedConf.getRequestHeaders());
   headers.put("Connection", "close");
   return headers;
 }
Beispiel #21
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();
  }
 DirectMessageJSONImpl(HttpResponse httpresponse, Configuration configuration)
 {
     super(httpresponse);
     httpresponse = httpresponse.asJSONObject();
     init(httpresponse);
     if (configuration.isJSONStoreEnabled())
     {
         TwitterObjectFactory.clearThreadLocalMap();
         TwitterObjectFactory.registerJSONObject(this, httpresponse);
     }
 }
    static ResponseList createSavedSearchList(HttpResponse httpresponse, Configuration configuration)
        throws TwitterException
    {
        int i;
        if (configuration.isJSONStoreEnabled())
        {
            TwitterObjectFactory.clearThreadLocalMap();
        }
        JSONArray jsonarray = httpresponse.asJSONArray();
        ResponseListImpl responselistimpl;
        JSONObject jsonobject;
        SavedSearchJSONImpl savedsearchjsonimpl;
        try
        {
            responselistimpl = new ResponseListImpl(jsonarray.length(), httpresponse);
        }
        // Misplaced declaration of an exception variable
        catch (Configuration configuration)
        {
            throw new TwitterException((new StringBuilder()).append(configuration.getMessage()).append(":").append(httpresponse.asString()).toString(), configuration);
        }
        i = 0;
_L2:
        if (i < jsonarray.length())
        {
            jsonobject = jsonarray.getJSONObject(i);
            savedsearchjsonimpl = new SavedSearchJSONImpl(jsonobject);
            responselistimpl.add(savedsearchjsonimpl);
            if (configuration.isJSONStoreEnabled())
            {
                TwitterObjectFactory.registerJSONObject(savedsearchjsonimpl, jsonobject);
            }
            break MISSING_BLOCK_LABEL_146;
        }
        if (configuration.isJSONStoreEnabled())
        {
            TwitterObjectFactory.registerJSONObject(responselistimpl, jsonarray);
        }
        return responselistimpl;
        i++;
        if (true) goto _L2; else goto _L1
  /**
   * @param conf configuration
   * @param supportsOAuth set true if the authorization instance required to be OAuth'ed
   * @return authorization instance
   * @deprecated use {@link #getInstance(twitter4j.conf.Configuration)} instead
   */
  public static Authorization getInstance(Configuration conf, boolean supportsOAuth) {
    Authorization auth = null;
    String consumerKey = conf.getOAuthConsumerKey();
    String consumerSecret = conf.getOAuthConsumerSecret();

    if (supportsOAuth && null != consumerKey && null != consumerSecret) {
      OAuthAuthorization oauth;
      oauth = new OAuthAuthorization(conf, consumerKey, consumerSecret);
      String accessToken = conf.getOAuthAccessToken();
      String accessTokenSecret = conf.getOAuthAccessTokenSecret();
      if (null != accessToken && null != accessTokenSecret) {
        oauth.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret));
      }
      auth = oauth;
    } else {
      String screenName = conf.getUser();
      String password = conf.getPassword();
      if (null != screenName && null != password) {
        auth = new BasicAuthorization(screenName, password);
      }
    }
    if (null == auth) {
      auth = NullAuthorization.getInstance();
    }
    return auth;
  }
 public void testLocation() throws Exception {
   JSONArray array = getJSONArrayFromClassPath("/dao/trends-available.json");
   ResponseList<Location> locations =
       LocationJSONImpl.createLocationList(array, conf.isJSONStoreEnabled());
   Assert.assertEquals(23, locations.size());
   Location location = locations.get(0);
   Assert.assertEquals("GB", location.getCountryCode());
   Assert.assertEquals("United Kingdom", location.getCountryName());
   Assert.assertEquals("United Kingdom", location.getName());
   Assert.assertEquals(12, location.getPlaceCode());
   Assert.assertEquals("Country", location.getPlaceName());
   Assert.assertEquals("http://where.yahooapis.com/v1/place/23424975", location.getURL());
   Assert.assertEquals(23424975, location.getWoeid());
 }
 public boolean equals(Object obj) {
   if (this != obj) {
     if (!(obj instanceof z_T4JInternalJSONImplFactory)) {
       return false;
     }
     obj = (z_T4JInternalJSONImplFactory) obj;
     if (conf == null
         ? ((z_T4JInternalJSONImplFactory) (obj)).conf != null
         : !conf.equals(((z_T4JInternalJSONImplFactory) (obj)).conf)) {
       return false;
     }
   }
   return true;
 }
Beispiel #27
0
 /** @param conf configuration */
 public OAuthAuthorization(final Configuration conf) {
   this.conf = conf;
   http = new HttpClientWrapper(conf);
   setOAuthConsumer(conf.getOAuthConsumerKey(), conf.getOAuthConsumerSecret());
   if (conf.getOAuthAccessToken() != null && conf.getOAuthAccessTokenSecret() != null) {
     setOAuthAccessToken(
         new AccessToken(conf.getOAuthAccessToken(), conf.getOAuthAccessTokenSecret()));
   }
 }
 @Override
 public int hashCode() {
   int result = client != null ? client.hashCode() : 0;
   result = 31 * result + (conf != null ? conf.hashCode() : 0);
   result = 31 * result + (apiKey != null ? apiKey.hashCode() : 0);
   result = 31 * result + (oauth != null ? oauth.hashCode() : 0);
   result = 31 * result + (uploadUrl != null ? uploadUrl.hashCode() : 0);
   result = 31 * result + (postParameter != null ? Arrays.hashCode(postParameter) : 0);
   result = 31 * result + (appendParameter != null ? Arrays.hashCode(appendParameter) : 0);
   result = 31 * result + (photo != null ? photo.hashCode() : 0);
   result = 31 * result + (message != null ? message.hashCode() : 0);
   result = 31 * result + (headers != null ? headers.hashCode() : 0);
   result = 31 * result + (httpResponse != null ? httpResponse.hashCode() : 0);
   return result;
 }
Beispiel #29
0
 /** {@inheritDoc} */
 @Override
 public AccessToken getOAuthAccessToken(final String oauthVerifier) throws TwitterException {
   ensureTokenIsAvailable();
   final String url = conf.getOAuthAccessTokenURL();
   if (0 == url.indexOf("http://")) {
     // SSL is required
     // @see https://dev.twitter.com/docs/oauth/xauth
     // url = "https://" + url.substring(7);
   }
   final String sign_url = conf.getSigningOAuthAccessTokenURL();
   if (0 == sign_url.indexOf("http://")) {
     // SSL is required
     // @see https://dev.twitter.com/docs/oauth/xauth
     // sign_url = "https://" + sign_url.substring(7);
   }
   oauthToken =
       new AccessToken(
           http.post(
               url,
               sign_url,
               new HttpParameter[] {new HttpParameter("oauth_verifier", oauthVerifier)},
               this));
   return (AccessToken) oauthToken;
 }
 public OAuthAuthorization(Configuration configuration)
 {
     consumerKey = "";
     realm = null;
     oauthToken = null;
     conf = configuration;
     http = new HttpClientWrapper(configuration);
     setOAuthConsumer(configuration.getOAuthConsumerKey(), configuration.getOAuthConsumerSecret());
     if (configuration.getOAuthAccessToken() != null && configuration.getOAuthAccessTokenSecret() != null)
     {
         setOAuthAccessToken(new AccessToken(configuration.getOAuthAccessToken(), configuration.getOAuthAccessTokenSecret()));
     }
 }