Beispiel #1
0
 public static void save(Twitter twitter)
 {
     clear();
     settings.Add(ACCESS_TOKEN, twitter.getAccessToken());
     settings.Add(SECRET_TOKEN, twitter.getSecretToken());
 }
Beispiel #2
0
        /// <summary>
        /// Sends request to Twitter
        /// </summary>
        /// <param name="url">Request url</param>
        /// <param name="mapParams">Request parameters</param>
        /// <param name="method">Request method</param>
        /// <param name="callback">Method to be called when request finishes</param>
        public static void request(String url, Dictionary <String, String> mapParams, String method, Action <String, NetmeraException> callback)
        {
            if (twitter != null && twitter.isSessionValid())
            {
                try
                {
                    var credentials = new OAuthCredentials
                    {
                        Type              = OAuthType.ProtectedResource,
                        SignatureMethod   = OAuthSignatureMethod.HmacSha1,
                        ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
                        ConsumerKey       = twitter.getConsumerKey(),
                        ConsumerSecret    = twitter.getConsumerSecret(),
                        Token             = twitter.getAccessToken(),
                        TokenSecret       = twitter.getSecretToken(),
                        Version           = "1.0",
                    };

                    var request = new RestRequest();
                    request.Credentials = credentials;
                    if (!String.IsNullOrEmpty(url))
                    {
                        request.Path = url.Replace("http://", "https://").Replace(Twitter.ApiHost, "");
                    }
                    if (!String.IsNullOrEmpty(method) && method.ToUpper().Trim() == "POST")
                    {
                        request.Method = WebMethod.Post;
                    }

                    if (mapParams != null && mapParams.Count != 0)
                    {
                        foreach (var key in mapParams.Keys)
                        {
                            request.AddParameter(key, mapParams[key]);
                        }
                    }

                    var client = new RestClient
                    {
                        Authority = Twitter.ApiHost,
                        HasElevatedPermissions = true
                    };

                    client.BeginRequest(request, new RestCallback((req, resp, userstate) =>
                    {
                        if (resp.StatusCode != HttpStatusCode.OK)
                        {
                            JObject json = JObject.Parse(resp.Content);
                            if (json["error"] != null)
                            {
                                if (callback != null)
                                {
                                    callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_TW_ERROR, json.Value <String>("error")));
                                }
                            }
                            else
                            {
                                if (callback != null)
                                {
                                    callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_TW_ERROR, "Request error occured."));
                                }
                            }
                        }
                        else
                        {
                            if (callback != null)
                            {
                                callback(resp.Content, null);
                            }
                        }
                    }));
                }
                catch
                {
                    if (callback != null)
                    {
                        callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_TW_ERROR, "Request error occured."));
                    }
                }
            }
            else
            {
                if (callback != null)
                {
                    callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_TW_ERROR, "Must first login."));
                }
            }
        }