tags:

views:

299

answers:

0

Hello guys,

here i am facing a problem when i am using facebook notification.sendEmail api. its generating the incorrect signature error i don't what happened there so please help me. here is code to which i used to generating the signature.

 Private Function GenerateSignature(ByRef Parameters As NameValueCollection) As String
        Dim i As Integer

        ' 1) Sort the parameters alphabetically by parameter name
        Dim SortedParamNames(Parameters.Count) As String
        For i = 0 To Parameters.Count - 1
            SortedParamNames(i) = Parameters.Keys(i)
        Next
        Array.Sort(SortedParamNames)

        ' 2) Assemble the parameters and values ("param=value") in a string
        '    without any separating characters between the parameters
        Dim Result As String = ""
        For i = 0 To SortedParamNames.Length - 1
            If SortedParamNames(i) <> "" Then
                Result &= SortedParamNames(i) & "=" & Parameters(SortedParamNames(i))
            End If
        Next

        ' 3) Tack the Secret value on the end
        Result = Trim(Result & m_Secret)

        Dim MD5Encoder As New MD5CryptoServiceProvider
        Dim MD5Bytes() As Byte
        Dim MD5Result As String = ""
        Dim ResultBytes() As Byte = ASCII.GetBytes(Result)
        MD5Bytes = MD5Encoder.ComputeHash(ResultBytes)

        ' 4) Take the hash of the above and translate it into a hexadecimal string
        For i = 0 To MD5Bytes.Length - 1
            Dim ThisByte As String = Hex(MD5Bytes(i))
            If Len(ThisByte) < 2 Then ThisByte = "0" & ThisByte
            MD5Result &= ThisByte
        Next

        ' 5) Return the string in lowercase
        Return MD5Result.ToLower
    End Function

And here is method which i used to call aan api

Private Function callRemoteMethod(ByVal MethodName As String, Optional ByVal Parameters As NameValueCollection = Nothing, Optional ByVal UseSSL As Boolean = False) As String
        ' If there weren't any parameters passed, instantiate a new collection for the parameters we need
        If Parameters Is Nothing Then
            Parameters = New NameValueCollection
        End If

        ' Randomly increment the request number (to stop replay attacks)
        m_RequestNumber += (1 + CInt(Rnd() * 10))

        ' Add the parameters all calls need
        Parameters.Add("api_key", m_APIKey)
        Parameters.Add("method", MethodName)
        Parameters.Add("session_key", m_SessionKey)
        Parameters.Add("call_id", m_RequestNumber)
        ' This value must always be updated to the latest version
        ' of the API supported by this class.
        Parameters.Add("v", "1.0")

        ' Generate the signature for all of the parameters above
        Dim Signature As String
        Try
            Signature = GenerateSignature(Parameters)
        Catch e As Exception
            Return ""
        End Try
        Parameters.Add("sig", Signature)

        ' Put the post string together for the Internet call
        Dim i As Integer
        Dim PostString As String = ""
        Dim WebClient As New WebClient
        For i = 0 To Parameters.Count - 1
            If PostString <> "" Then PostString &= "&"
            PostString &= Parameters.Keys(i) & "=" & Parameters.Item(i)
        Next

        ' Add the encoding for the server request
        WebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
        Dim PostBytes() As Byte = ASCII.GetBytes(PostString)
        Dim ResponseBytes() As Byte
        Try
            Dim Address As String
            ' If the call needs to be secure, use HTTPS and not HTTP.
            If UseSSL = False Then
                Address = "http://" & ServerURL
            Else
                Address = "https://" & ServerURL
            End If

            ResponseBytes = WebClient.UploadData(Address, "POST", PostBytes)
        Catch e As Exception
            Return ""
        End Try

        ' Return the response from the server
        Return ASCII.GetString(ResponseBytes)
    End Function

please tell me something to solve this problem.

related questions