예제 #1
0
        public Status UpdateStatus(string status)
        {
            if (status == null)
            {
                throw new ArgumentNullException("status");
            }

            if (string.IsNullOrEmpty(status))
            {
                throw new ArgumentException(
                          "status must not be empty",
                          "status");
            }

            if (status.Length > 140)
            {
                throw new ArgumentException(
                          "status must not be more than 140 characters",
                          "status");
            }

            OAuthResource resource = this.ExecuteRequest(
                TwitterApi.ServiceDefinition,
                new NameValueCollection
            {
                { "status", status }
            },
                "http://twitter.com/statuses/update.xml", "POST");

            return(Status.Deserialize(resource.GetResponseStream()));
        }
예제 #2
0
        /// <summary>
        /// Verifies the user credentials and fetches extended user information
        /// for the current user.
        /// </summary>
        /// <param name="user">(Out) extended user information</param>
        /// <param name="options">API options</param>
        /// <returns>
        /// <para>
        /// Returns <c>true</c> and sets <paramref name="user"/> to a
        /// representation of the requesting user if authentication was
        /// successful.
        /// </para>
        /// <para>
        /// Returns <c>false</c> and sets <paramref name="user"/> to null
        /// if authentication was not successful.
        /// </para>
        /// </returns>
        public bool VerifyCredentials(out ExtendedUser user)
        {
            OAuthResource resource = this.ExecuteRequest(
                TwitterApi.ServiceDefinition,
                null,
                "http://twitter.com/account/verify_credentials.xml", "GET");

            switch (resource.StatusCode)
            {
            case HttpStatusCode.Unauthorized:
                user = null;
                return(false);

            case HttpStatusCode.OK:
                user = ExtendedUser.Deserialize(resource.GetResponseStream());
                return(true);

            default:
                throw new InvalidOperationException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              "Unexpected response from Twitter account/verify_credentials.xml: {0}",
                              resource.StatusCode));
            }
        }
예제 #3
0
        /// <summary>
        /// Retrieves the 20 most recent statuses posted from the authenticated
        /// user.
        /// </summary>
        /// <param name="options">API options</param>
        /// <returns>List of statuses</returns>
        public ReadOnlyCollection <Status> UserTimeline()
        {
            OAuthResource resource = this.ExecuteRequest(
                TwitterApi.ServiceDefinition,
                null,
                "http://twitter.com/statuses/user_timeline.xml", "GET");

            return(new ReadOnlyCollection <Status>(
                       new ListSerializationHelper().DeserializeStatusList(
                           resource.GetResponseStream())));
        }
예제 #4
0
        private OAuthResponse GetResource(NameValueCollection parameters, string contentType, System.IO.Stream bodyStream)
        {
            OAuthResponse response;

            HttpWebRequest request = this.PrepareProtectedResourceRequest(parameters, contentType, bodyStream);

            // A null value for the HttpWebRequest is returned when a ResponseToken is returned
            // and no one has returned in the AuthorizationHandler continue with getting an AccessToken
            // or an RequestToken exists but the AccessToken request was refused.
            if (request == null)
            {
                response = new OAuthResponse(this.RequestToken);
            }
            else
            {
                OAuthResource   resource;
                OAuthParameters responseParameters;

                try
                {
                    resource = new OAuthResource((HttpWebResponse)request.GetResponse());

                    // Parse the parameters and re-throw any OAuthRequestException from the service provider
                    responseParameters = OAuthParameters.Parse(resource);
                    OAuthRequestException.TryRethrow(responseParameters);

                    // If nothing is thrown then we should have a valid resource.
                    response = new OAuthResponse(this.AccessToken ?? this.RequestToken, resource);
                }
                catch (WebException e)
                {
                    // Parse the parameters and re-throw any OAuthRequestException from the service provider
                    responseParameters = OAuthParameters.Parse(e.Response as HttpWebResponse);
                    OAuthRequestException.TryRethrow(responseParameters);

                    // If no OAuthRequestException, rethrow the WebException
                    #warning TODO: We have consumer the WebException's body so rethrowing it is pretty pointless; wrap the WebException in an OAuthProtocolException and store the body (create an OAuthResource before parsing parameters)
                    throw;
                }
            }

            return(response);
        }
예제 #5
0
 internal OAuthResponse(IToken token, OAuthResource resource)
 {
     this.Token                = token;
     this.ProtectedResource    = resource;
     this.HasProtectedResource = resource != null;
 }
 public string ResourceHtml(OAuthToken token, OAuthResource resource, OAuthApplication application)
 {
     return(string.Empty);
 }