public void TestBuildAuthorizationUriWithExistingQueryString() { OAuthService service = OAuthService.Create( new EndPoint("http://example.com/request_token"), new Uri("http://example.com/authorize?readonly=true"), new EndPoint("http://example.com/access_token"), new MockConsumer() { Key = "key", Secret = "secret", Status = ConsumerStatus.Valid }); Uri authUri = service.BuildAuthorizationUrl( new OAuthToken(TokenType.Request, "token", null, (string)null)); Assert.That( authUri.AbsoluteUri, Is.EqualTo("http://example.com/authorize?readonly=true&oauth_token=token")); }
public void TestBuildAuthorizationUriWithAdditionalParametersWithoutExistingQueryString() { OAuthService service = OAuthService.Create( new EndPoint("http://example.com/request_token"), new Uri("http://example.com/authorize"), new EndPoint("http://example.com/access_token"), new MockConsumer() { Key = "key", Secret = "secret", Status = ConsumerStatus.Valid }); Uri authUri = service.BuildAuthorizationUrl( new OAuthToken(TokenType.Request, "token", null, (string)null), new NameValueCollection { { "time", "60" } }); Assert.That( authUri.AbsoluteUri, Is.EqualTo("http://example.com/authorize?oauth_token=token&time=60")); }
public string Access(string url) { // Create OAuthService object, containing oauth consumer configuration service = OAuthService.Create( new EndPoint(RequestTokenUrl, "POST"), // requestTokenEndPoint new Uri(AuthorizationUrl), // authorizationUri new EndPoint(AccessTokenUrl, "POST"), // accessTokenEndPoint true, // useAuthorizationHeader "http://wbsapi.withings.net", // realm "HMAC-SHA1", // signatureMethod "1.0", // oauthVersion new OAuthConsumer(ConsumerKey, ConsumerSecret) // consumer ); string content; try { // Create OAuthRequest object, providing protected resource URL OAuthRequest request; if (m_bAuthorized) { request = OAuthRequest.Create( new EndPoint(url, "GET"), service, callbackurl, requestToken, accessToken); } else { request = OAuthRequest.Create( new EndPoint(url, "GET"), service, callbackurl, sessionId); } // Assign verification handler delegate request.VerificationHandler = AspNetOAuthRequest.HandleVerification; // Call OAuthRequest object GetResource method, which returns OAuthResponse object OAuthResponse response = request.GetResource(); // Check if OAuthResponse object has protected resource if (!response.HasProtectedResource) { m_bAuthorized = false; // If not we are not authorized yet, build authorization URL and redirect to it string authorizationUrl = service.BuildAuthorizationUrl(response.Token).AbsoluteUri; return(authorizationUrl); } else { //Save our data requestToken = request.RequestToken; accessToken = request.AccessToken; } // Store the access token in session variable access_token = response.Token; // Initialize the XmlDocument object and OAuthResponse object's protected resource to it m_bAuthorized = true; Stream ms = response.ProtectedResource.GetResponseStream(); // Jump to the start position of the stream ms.Seek(0, SeekOrigin.Begin); StreamReader rdr = new StreamReader(ms); string anwser_content = rdr.ReadToEnd(); return(anwser_content); } catch (OAuthRequestException ex) { return(ex.Message); } }