public void TestSuccess1() { OAuthService service = new OAuthService(new OAuthEndPoint(RequestTokenAddress), AuthorizationAddress, new OAuthEndPoint(AccessTokenAddress), Consumer); OAuthClientContext clientContext = new OAuthClientContext(service); clientContext.NonceGenerator = new GuidNonceGenerator(); UriBuilder builder = new UriBuilder(); builder.Scheme = Uri.UriSchemeHttp; builder.Host = PathServiceAddress.Host; builder.Port = PathServiceAddress.Port; builder.Path = PathName; OAuthRequest consumerRequest = clientContext.CreateRequest(new OAuthEndPoint(builder.ToString(), "GET")); OAuthResponse resource = consumerRequest.GetResource(); if (resource.HasProtectedResource) { //TODO: } }
private OAuthResponse GetResource(NameValueCollection parameters, string contentType, Stream bodyStream) { OAuthResponse response; HttpWebRequest request = 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(RequestToken); } else { OAuthParameters responseParameters; try { OAuthResource 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(AccessToken ?? 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); }
private OAuthResponse GetResource(NameValueCollection parameters, string contentType, Stream bodyStream) { OAuthResponse response; HttpWebRequest request = 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(RequestToken); else { OAuthParameters responseParameters; try { OAuthResource 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(AccessToken ?? 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; }