我们有一个使用 vb.net 的 oauth 雅虎电子邮件验证器的小项目,我们对 oauth 实现非常陌生,想问一下我们的代码是否正确,我们没有得到服务器的响应,希望我们能得到一些很好的答案在这里提前谢谢
下面是附上的代码
Option Explicit On
Option Strict On
Public Class frmValidator
Public Const YahooAppID As String = "yPh6rr56" 'pfswebdev@yahoo.com/pfsweb123
Public Const YahooConsumerKey As String = "dj0yJmk9Qkw3VWFZcXA0V1ZNJmQ9WVdrOWVWQm9Obkp5TlRZbWNHbzlNakEzTWpNek5qVTJNZy0tJnM9Y29uc3VtZXJzZWNyZXQmeD0wNA--"
Public Const YahooConsumerSecret As String = "80e148890ec0a31da518013998fbbfd49b996296"
Public Const YQLAddress As String = "http://query.yahooapis.com/v1/public/yql"
Public Shared Function VerifyYahooAccount(ByVal emailAddress As String) As String
Dim yahooID As String = emailAddress.Substring(0, emailAddress.IndexOf("@"))
Dim restURL As String = "http://query.yahooapis.com/v1/yql?q=%20select%20*%20from%20social.profile%20where%20guid%20in%20(select%20guid%20from%20yahoo.identity%20where%20yid%20%3D%20'" & yahooID & "')&diagnostics=false"
Dim requestUrl As String = ""
Dim normalizedUrl As String = ""
Dim normalizedUrlParams As String = ""
Dim provider As OAuth.OAuthBase = New OAuth.OAuthBase()
Dim signature As String = provider.GenerateSignature(New Uri(restURL), YahooConsumerKey, YahooConsumerSecret, "", "", "GET", provider.GenerateTimeStamp(), provider.GenerateNonce(), normalizedUrl, normalizedUrlParams)
requestUrl = normalizedUrl & "?" & normalizedUrlParams & "&" & Web.HttpUtility.UrlEncode("oauth_signature") & "=" & Web.HttpUtility.UrlEncode(signature)
Dim context As New Consumer.OAuthConsumerContext()
With context
.ConsumerKey = YahooConsumerKey
.ConsumerSecret = YahooConsumerSecret
.SignatureMethod = DevDefined.OAuth.Framework.SignatureMethod.HmacSha1
.UseHeaderForOAuthParameters = True
End With
Dim session As New Consumer.OAuthSession(context, "https://api.login.yahoo.com/oauth/v2/get_request_token", "https://api.login.yahoo.com/oauth/v2/request_auth", "https://api.login.yahoo.com/oauth/v2/get_token")
Dim response As HttpWebResponse = session.Request.Get().ToWebResponse()
Dim xdoc As System.Xml.Linq.XDocument = New Consumer.ConsumerRequest(Nothing, Nothing, Nothing).GetRequestDescription
Dim responseData As String = ""
Try
Dim hwrequest As Net.HttpWebRequest = CType(Net.WebRequest.Create(requestUrl), Net.HttpWebRequest)
hwrequest.Accept = "text/xml"
hwrequest.AllowAutoRedirect = True
hwrequest.Timeout = 60000
hwrequest.Method = "GET"
Dim hwresponse As Net.HttpWebResponse = CType(hwrequest.GetResponse(), Net.HttpWebResponse)
If hwresponse.StatusCode = Net.HttpStatusCode.OK Then
Dim responseStream As IO.StreamReader = _
New IO.StreamReader(hwresponse.GetResponseStream())
responseData = responseStream.ReadToEnd()
End If
hwresponse.Close()
Catch e As Exception
responseData = "An error occurred: " & e.Message
End Try
End Function
Private Sub btnValidate_Click(sender As System.Object, e As System.EventArgs) Handles btnValidate.Click
VerifyYahooAccount(txtValidate.Text.ToString)
End Sub
End Class