Add your own alternative version
Stats
228.2K views 7.3K downloads 52 bookmarked
Posted
29 Aug 2011
|
Comments and Discussions
|
|
|
It is a helpfull article!
I have a sample of a php code, to get a json file. I tried to change the code of the article and to work on vb but not succesfuly. It would be great if i had some help to go to the next step. I will show the php code sample, and the vb code that I wrote. Thanks for any help!
Oh! Sorrry for my bad english level...
Php Code :
<?php
$base_url = 'http://www.inart.com/';
$gr_url = $base_url . 'api/rest/products/store/1';
$en_url = $base_url . 'api/rest/products/store/2';
$consumer_key = 'CONSUMER_KEY';
$consumer_secret = 'CONSUMER_SECRET';
$token = 'TOKEN';
$token_secret = 'TOKEN_SECRET';
$parameters = array();
$headers = array(
'Content-Type' => 'application/json',
'Accept' => 'application/json'
);
$filters = array(
'limit' => 20,
'page' => 1
);
$url = $gr_url . '?' . http_build_query($filters);
try {
$oauthClient = new OAuth($consumer_key, $consumer_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
$oauthClient->setToken($token, $token_secret);
$oauthClient->fetch($url, $parameters, OAUTH_HTTP_METHOD_GET, $headers);
$json = $oauthClient->getLastResponse();
echo $json;
} catch (Exception $e) {
echo $e->getMessage();
}
Vb code :
Dim consumer_key = "3626311748bcf2072da2bd475fcxxxxx"
Dim consumer_secret = "0cbb0df8d840e22b96d4f80449exxxxx"
Dim token = "82c43c4cbff206316428860ee25xxxxx"
Dim token_secret = "986d1996d22ccffa51dc960c041xxxxx"
Dim oauth_version = "1.0"
Dim oauth_signature_method = "HMAC-SHA1"
Dim base_url = "http://dev.inart.gr/api/rest/products/store/1"
Dim baseFormat = "consumer_key={0}&oauth_signature_method={1}" + "&token={2}&oauth_version={3}"
Dim baseString = String.Format(baseFormat, consumer_key, oauth_signature_method, token, oauth_version)
baseString = String.Concat("POST&", Uri.EscapeDataString(base_url), "&", Uri.EscapeDataString(baseString))
Dim compositeKey = String.Concat(Uri.EscapeDataString(consumer_secret), "&", Uri.EscapeDataString(token_secret))
Dim oauth_signature As String
Using hasher As New HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey))
oauth_signature = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)))
End Using
Dim headerFormat = "OAuth oauth_signature_method=""{0}"", " + "consumer_key=""{1}"", " + "oauth_token=""{2}"", oauth_signature=""{3}"", " + "oauth_version=""{4}"""
Dim authHeader = String.Format(headerFormat, Uri.EscapeDataString(oauth_signature_method), Uri.EscapeDataString(consumer_key), Uri.EscapeDataString(token),
Uri.EscapeDataString(oauth_signature), Uri.EscapeDataString(oauth_version))
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(base_url), HttpWebRequest)
request.Headers.Add("Authorization", authHeader)
request.Method = "POST"
request.ContentType = "application/json"
request.Accept = "application/json"
Dim response As WebResponse = request.GetResponse()
MsgBox(response.ToString)
|
|
|
|
|
I am getting below error near "WebResponse response = request.GetResponse();"
The remote server returned an error: (401) Unauthorized."
please help me
|
|
|
|
|
So am I... Even tried creating a new app and using those credentials. My credentials work with the API console and curl.
|
|
|
|
|
Hi, can you help me how to add image in the post? I think I was able to upload a picture and get the media ID back, but I don't know how to add it in the post after.
|
|
|
|
|
|
getting below error near "WebResponse response = request.GetResponse();"
The remote server returned an error: (401) Unauthorized."
please help me
Thankyou
|
|
|
|
|
this doesnot work.
It gives me an 401 error.
has any body got this worked/?
|
|
|
|
|
I'm writing a sample how to use Twitter with our Crawler-Lib Engine. Your sample is a great starting point for the authentication stuff. Thanks a lot!
|
|
|
|
|
Hi I've used VBA to tweet using this code: http://greglib.org/tweeting-with-vba/[^]
Which worked great. However recently I have ported my application over to VB.NET, and used the code in this thread.
Although it tweets, it has a problem:
If you need to tweet several tweets in sequence, it will tweet twice, and then times out.
Yes, each tweet has a different message text.
I suspect there is something wrong with the HttpWebRequest, but I have no knowledge on the matter.
Doing exactly the same with the abovementioned VBA code in MS access works just fine. for example I tweet 10 messages in sequence; works with the VBA code, but with this code, it tweets two messages, and times out.
any ideas?
|
|
|
|
|
 Did you get anywhere with this?
I have the exact same problem trying to send tweets using VB.Net. It will send 2 tweets and then timeout for any further tweets.
I used Wireshark to have a look at the packets and it looks like they might be malformed in some way. But very bizarre that two tweets always appear and the rest then timeout.
Here is my code:
Public Class SBTwitter
Private oauth_token As String
Private oauth_token_secret As String
Private oauth_consumer_key As String
Private oauth_consumer_secret As String
Public Sub New(ByVal APIKey As String, ByVal APISecret As String, ByVal oauthToken As String, ByVal oauthTokenSecret As String)
oauth_token = oauthToken
oauth_token_secret = oauthTokenSecret
oauth_consumer_key = APIKey
oauth_consumer_secret = APISecret
End Sub
Public Function PostInTwitter(ByVal post As String) As String
Try
Dim oauth_version = "1.0"
Dim oauth_signature_method = "HMAC-SHA1"
Dim oauth_nonce = Convert.ToBase64String(New ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()))
Dim timeSpan = DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)
Dim oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString()
Dim resource_url = "https://api.twitter.com/1.1/statuses/update.json"
Dim status = post
Dim baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&status={6}"
Dim baseString = String.Format(baseFormat, oauth_consumer_key, oauth_nonce, oauth_signature_method, oauth_timestamp, oauth_token, oauth_version, Uri.EscapeDataString(status))
baseString = String.Concat("POST&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString))
Dim compositeKey = String.Concat(Uri.EscapeDataString(oauth_consumer_secret), "&", Uri.EscapeDataString(oauth_token_secret))
Dim oauth_signature As String
Using hasher As New HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey))
oauth_signature = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)))
End Using
Dim headerFormat = "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 = 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))
Dim postBody = "status=" & Uri.EscapeDataString(status)
ServicePointManager.Expect100Continue = False
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(resource_url), HttpWebRequest)
request.Headers.Add("Authorization", authHeader)
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
Using stream As Stream = request.GetRequestStream()
Dim content As Byte() = ASCIIEncoding.ASCII.GetBytes(postBody)
stream.Write(content, 0, content.Length)
End Using
Dim response As WebResponse = request.GetResponse()
Console.WriteLine(response)
Return response.ToString
Catch ex As Exception
Console.WriteLine(ex.Message)
Return ex.Message
End Try
End Function
End Class
|
|
|
|
|
|
 Yes it was very frustrating but got it working finally.
code:
Imports System.Text
Imports System.Security.Cryptography
Public Class Tweet
Public Const cnsAPIMethodP As String = "POST"
Public Const cnsAPIMethodG As String = "GET"
Public Shared Function UrlEncode(str As String)
Dim objEnc As MSScriptControl.ScriptControl
objEnc = New MSScriptControl.ScriptControl
objEnc.Language = "JScript"
objEnc.AddCode("function encode(str) {return encodeURIComponent(str);}")
Dim encoded As String
encoded = objEnc.Run("encode", str)
UrlEncode = encoded
End Function
Public Shared Function send_tweet(strStatus As String) As Boolean
Dim boolResult As Boolean = False
Dim oauth_consumer_key As String = "xxxxxxxxxxx"
Dim oauth_consumer_secret As String = "xxxxxxxxxxx"
Dim oauth_token As String = "xxxxxxxxxxx"
Dim oauth_token_secret As String = "xxxxxxxxxxx"
Dim oauth_version = "1.0"
Dim oauth_signature_method = "HMAC-SHA1"
Dim oauth_nonce = Convert.ToBase64String(New ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()))
Dim timeSpan = DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0, _
0, DateTimeKind.Utc)
Dim oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString()
Dim resource_url = "https://api.twitter.com/1.1/statuses/update.json"
Dim baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" & "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&status={6}"
Dim baseString = String.Format(baseFormat, oauth_consumer_key, oauth_nonce, oauth_signature_method, oauth_timestamp, oauth_token, _
oauth_version, Uri.EscapeDataString(strStatus))
baseString = String.Concat("POST&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString))
Dim compositeKey = String.Concat(Uri.EscapeDataString(oauth_consumer_secret), "&", Uri.EscapeDataString(oauth_token_secret))
Dim oauth_signature As String
Dim objHasher As HMACSHA1 = New HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey))
oauth_signature = Convert.ToBase64String(objHasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)))
Dim headerFormat = "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 = 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)
)
strStatus = UrlEncode(strStatus)
Dim objRest As WinHttp.WinHttpRequest
objRest = New WinHttp.WinHttpRequest
objRest.Open(cnsAPIMethodP, resource_url, False)
objRest.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
objRest.SetRequestHeader("Authorization", authHeader)
objRest.Send("status=" & strStatus)
objRest.WaitForResponse()
Debug.Print(objRest.Status & " -- " & objRest.StatusText & " -- " & objRest.ResponseText)
If objRest.Status = "200" Then
send_tweet = True
Else
send_tweet = False
End If
objRest = Nothing
End Function
End Class
|
|
|
|
|
What is the change you made?
May you live the rest of your day...
|
|
|
|
|
Hello just we have to do one thing is that you have to just update message that's it each and every time you have to change your message whatever you want tweet and code is working good.
|
|
|
|
|
My application received the error 401. How can solve this problem?
Anybody can help me
William Costa Rodrigues
Rio de Janeiro, RJ, Brazil
modified 18-Nov-14 14:09pm.
|
|
|
|
|
Hi! It works ok for me. I need to change to vb.net and have to change de header format to work, like this:
'Authentication header
Dim headerFormat As String = "OAuth oauth_consumer_key=""{0}"", oauth_nonce=""{1}"", " +
"oauth_signature=""{2}"", oauth_signature_method=""{3}"", " +
"oauth_timestamp=""{4}"", oauth_token=""{5}"", " +
"oauth_version=""{6}"""
Dim authHeader As String = String.Format(headerFormat,
Uri.EscapeDataString(oauth_consumer_key),
Uri.EscapeDataString(oauth_nonce),
Uri.EscapeDataString(oauth_signature),
Uri.EscapeDataString(oauth_signature_method),
Uri.EscapeDataString(oauth_timestamp),
Uri.EscapeDataString(oauth_token),
Uri.EscapeDataString(oauth_version)
)
|
|
|
|
|
I am getting the error as The remote server returned an error: (403) Forbidden.
on posting what should i do.
I changed the following as per my app id and secret key is there any other thing need to be changed
oauth_token
oauth_token_secret
oauth_consumer_key
oauth_consumer_secret
|
|
|
|
|
You change var resource_url = "http://api.twitter.com/1/statuses/update.json"; to var resource_url = "https://api.twitter.com/1.1/statuses/update.json";
detail: http = https
1 = 1.1
|
|
|
|
|
Yorumlara göre işlemleri denedim ama hala 401 hatası geliyor.
Please help me, thanks. Yardımınızı bekliyorum teşekkürler.
resource_url in doğru adresi nedir? Yorumlarda başka, makalede başka yazanlar var.
Also, What is true resource_url?
|
|
|
|
|
Firstly as mentioned earlier in the resource_url change 1 to 1.1
var resource_url = "https://api.twitter.com/1.1/statuses/update.json";
Secondly change http to https
That should do it
|
|
|
|
|
|
|
|
an error appear:The remote server returned an error: (403) Forbidden.
|
|
|
|
|
now working for me
Indonesia IT Intelijensi
Freedom of Revealing And Sharing Knowledge.
www.indonesiaitintelijensi.com
|
|
|
|
|
|
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
|