Bonsoir,
J'ai qlq soucis avec Oauth pour twitter.
Ci-dessous le code pour la création de l'OAuth et l'envoie de la requeste :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 
    Public oauth_consumer_key As String = "xxxx"
    Public oauth_consumer_secret As String = "xxxx"
    Public oauth_token As String = "xxxx-xxxxxxx"
    Public oauth_token_secret As String = "xxxxxx"
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        UpdateAuthorization()
    End Sub
 
    Sub UpdateAuthorization()
        Dim url As String = "https://api.twitter.com/1.1/search/tweets.json?q=SpaceBabies&lang=en&result_type=mixed&count=2"
 
        '==========================
        'GET TOKEN SIGNATURE OAUTH
        Dim oauth_version As String = "1.0"
        Dim oauth_signature_method As String = "HMAC-SHA1"
        Dim oauth_nonce As String = Convert.ToBase64String(New ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()))
 
        Dim timeSpan As TimeSpan = DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)
        Dim oauth_timestamp As String = Convert.ToInt64(timeSpan.TotalSeconds).ToString()
 
        Dim oauth_signature As String = GeneraOAuthSignature(url, _
                                                              oauth_nonce, _
                                                              oauth_signature_method, _
                                                              oauth_timestamp, _
                                                              oauth_version)
        '==========================
        'HEADER FORMAT OAUTH
        Dim headerFormat As String = "OAuth oauth_nonce=""{0}"", oauth_signature_method=""{1}"", oauth_timestamp=""{2}"", oauth_consumer_key=""{3}"", oauth_token=""{4}"", oauth_signature=""{5}"", oauth_version=""{6}"""
 
        Dim authHeader As String = String.Format(headerFormat, Uri.EscapeDataString(oauth_nonce), Uri.EscapeDataString(oauth_signature_method), Uri.EscapeDataString(oauth_timestamp), Uri.EscapeDataString(oauth_consumer_key), _
                    Uri.EscapeDataString(oauth_token), Uri.EscapeDataString(oauth_signature), Uri.EscapeDataString(oauth_version))
 
        '==========================
        'HTTP REQUEST
        ServicePointManager.Expect100Continue = False
 
        Dim req As WebRequest
 
        req = WebRequest.Create(url)
        req.Timeout = -1
        req.Headers.Add("Authorization", authHeader)
 
        ' c'est ici que ça se plante généralement ********** erreur 401 probleme d'OAuth
        Dim response As WebResponse = req.GetResponse()
        ' Get the stream containing content returned by the server.
        Dim dataStream As Stream = response.GetResponseStream()
        ' Open the stream using a StreamReader for easy access.
        Dim reader As New StreamReader(dataStream)
        ' Read the content.
        Dim responseFromServer As String = reader.ReadToEnd()
        ' Clean up the streams and the response.
        req.Abort()
        reader.Close()
        response.Close()
 
        RichTextBox1.Text = responseFromServer
    End Sub
 
    Function GeneraOAuthSignature(ByVal stream_url As String, _
                              ByVal oauth_nonce As String, _
                              ByVal oauth_signature_method As String, _
                              ByVal oauth_timestamp As String, _
                              ByVal oauth_version As String) As String
        'The next step is to generate an encrypted oAuth signature which Twitter will use to validate the request.
        'To do this, all of the request data is concatenated into a particular format as follows
        Dim baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" & _
                         "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}"
 
        Dim baseString As String = String.Format(baseFormat, _
                        oauth_consumer_key, _
                        oauth_nonce, _
                        oauth_signature_method, _
                        oauth_timestamp, _
                        oauth_token, _
                        oauth_version)
 
        baseString = String.Concat("GET&", Uri.EscapeDataString(stream_url), "&", Uri.EscapeDataString(baseString))
 
        'Using this base string, we then encrypt the data using a composite of the secret keys and the HMAC-SHA1 algorithm.
        Dim compositeKey As String = String.Concat(Uri.EscapeDataString(oauth_consumer_secret), "&", Uri.EscapeDataString(oauth_token_secret))
        Dim oauth_signature As String
 
        Dim hasher As HMACSHA1 = New HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey))
        Using hasher
            oauth_signature = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)))
        End Using
 
        Return oauth_signature
    End Function
Tout ce code semble fonctionner car les codes OAuth fonctionnent, testé sur le site twitter application.
Je ne comprends pas ce qui se passe.
je ne sais pas si c'est l'OAuth, la construction de la signature, la requete, la webrequest ou encore la response...

Avez-vous une idée car là, ça fait 2 jours que je suis dessus et je ne sais plus quoi faire
Deplus, je dois récupérer ce flux en XML et donc le parser par la suite, mais là je pourrais me débrouiller

Merci de votre aide