Authorization failure


#1

401:Authentication credentials (https://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have set valid consumer key/secret, access token/secret, and the system clock is in sync.
Failed to validate oauth signature and token

Relevant discussions can be found on the Internet at:
http://www.google.co.jp/search?q=8e063946 or
http://www.google.co.jp/search?q=ef59cf90
TwitterException{exceptionCode=[8e063946-ef59cf90], statusCode=401, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=4.0.1}
Failed to get timeline: 401:Authentication credentials (https://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have set valid consumer key/secret, access token/secret, and the system clock is in sync.
Failed to validate oauth signature and token

at twitter4j.HttpClientImpl.handleRequest(HttpClientImpl.java:164)
at twitter4j.HttpClientBase.request(HttpClientBase.java:53)
at twitter4j.HttpClientBase.post(HttpClientBase.java:82)
at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:116)
at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:98)
at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:287)
at joo.App.main(App.java:43)

Everytime i want to authenticate my app using API, aforementioned error arises.

Why this happens? anybody please tell me.


#2

Per the error message, have you ensured “that you have set valid consumer key/secret, access token/secret, and the system clock is in sync”?

The links you provided lead to some discussion suggesting that a proxy server may be to blame. Are you running your code behind a proxy server? If so, are you providing proxy details in the twitter4j configuration?

If the above doesn’t help,

  • can you provide the code from your joo.App class?

  • are you able to successfully use twurl to exercise the Twitter API?


#3

I am having a similar issue "The remote server returned an error: (401) Unauthorized."
I regenerated the 4 tokens consumer key/secret, access token/secret… now: "The remote server returned an error: (403) Forbidden"
Clock is in sync. I am not using a proxy server.
Access Level= Read, write, and direct messages.
also tried resource_url = https://api.twitter.com/oauth/authorize

I am using VB.net and http as follows. Note: I have changed the “secret” values:

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Net
Imports System.Security.Cryptography
Imports System.IO

Namespace TwitterOAuth
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            ' oauth application keys

            Dim oauth_token = "4V9429466-zavYyiWdI7MioetnmGSaGiMmn2wYSz6WCVNuNQcl"
            Dim oauth_token_secret = "ZicaWhqVTRVSVTQLpt67MKN4N9afMMwsRCy4MVxjlW4Ze."
            Dim oauth_consumer_key = "KpMNAVFX4ghsE64uVptFsmkQ0"
            Dim oauth_consumer_secret = "NQLxfzH6cUlEqpEfH0E4ulVkFNKWoI44N4vz7uK4ghaaSSezUX"

            ' oauth implementation details
            Dim oauth_version = "1.0"
            Dim oauth_signature_method = "HMAC-SHA1"

            ' unique request details
            Dim oauth_nonce = Convert.ToBase64String((New ASCIIEncoding()).GetBytes(Date.Now.Ticks.ToString()))
            Dim timeSpan = Date.UtcNow - New Date(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)
            Dim oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString()

            ' message api details
            Dim status = "Hoping to be authorized"
            Dim resource_url = "https://api.twitter.com/1.1/"

            ' create oauth signature
            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

            ' create the request header
            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))


            ' make the request aka "command"
            Dim postBody = "status=" & Uri.EscapeDataString(status)

            ServicePointManager.Expect100Continue = False

            Dim request As HttpWebRequest = CType(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()  '<<<<<<<<<,The remote server returned an error: (401) Unauthorized.

' ****** HERE ARE SOME VARIABLE VALUES (modified to protect the actual secret values) 
'composit key= "NQLxfzH6cUlEqpEfH0E4ulVkFNKWoI44N4vz7uK4ghaaSSezUX&ZicaWhqVTRVSVTQLpt67MKN4N9afMMwsRCy4MVxjlW4Ze.

"'resource_url = https://api.twitter.com/oauth/authorize
'authHeader="WAuth oauth_nonce="NjM4NTUzNzkwMTIwNjAyNSg2", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1419802070", oauth_consumer_key="KpMNAVFX4ghsE64uVptFsmkQ0", oauth_token="4V9429466-zavYyiWdI7MioetnmGSaGiMmn2wYSz6WCVNuNQcl", oauth_signature="96Y44LNF%2F%2FzdczFqRV06Uk0TSkM%VS", oauth_version="1.0""
'postBody="status=Hoping%20to%20Ne%20authorized"

        End Sub
    End Class
End Namespace

#4

I tried another way with the same result:
An unhandled exception of type ‘System.Net.WebException’ occurred in System.dll

Additional information: The remote server returned an error: (403) Forbidden…

 Dim req As WebRequest
 Dim res As HttpWebResponse

req = WebRequest.Create(resource_url)
req.Timeout = -1
req.Headers.Add("Authorization", authHeader)

 res = DirectCast(req.GetResponse(), HttpWebResponse)
Dim streamReader As StreamReader
Dim strLine As String
Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")

StreamReader = New StreamReader(res.GetResponseStream(), encode)
While True
strLine = streamReader.ReadLine()
End While

#5

Just tried https://api.twitter.com/oauth/access_token. which gives same UnAuthorized error. First off which URL is correct. thanks for reading.


#6

Of course, I have regenerated the keys several times as I stated. Clock is in sync. Is there anything else you can think of? Just for the heck of it I regenerated tokens and tried again… what is the stupid thing I am doing or not doing… please advise…


#7

ERROR: AT + req.GetResponse() {“The remote server returned an error: (401) Unauthorized.”} System.Net.WebException

Not sure why you are quoting Twitter4J Should I have started a new Thread?.. I am using native http. please see code.

        '------------------------------- FIRST TRY
        Dim request As HttpWebRequest = CType(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()  '<<<<<<<<<,The remote server returned an error: (401) Unauthorized.
     


        ''=============================== SECOND TRY
        Dim req As WebRequest
        Dim res As HttpWebResponse

        req = WebRequest.Create(resource_url)
        req.Timeout = -1
        req.Headers.Add("Authorization", authHeader)




        res = DirectCast(req.GetResponse(), HttpWebResponse)
        Dim streamReader As StreamReader
        Dim strLine As String
        Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")

        streamReader = New StreamReader(res.GetResponseStream(), encode)
        While True
            strLine = streamReader.ReadLine()

        End While

        '====================================

My settings: PLEASE CHECK IF I HAVE THE CORRECT SETTINGS…THANK YOU
’ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Here is the URL: resource_url = " https://api.twitter.com/oauth/authorize"
This is the header I am sending.

"OAuth oauth_nonce=“NjM1NTU0NDMxzzzzzMjM1MDcz”, oauth_signature_method=“HMAC-SHA1”, oauth_timestamp=“1419864385”, oauth_consumer_key=“N3s5Q1jdVrjP2BBBB7eL7efAw”, oauth_token=“859829466-zavYyiWdI7MiQWERTGSaGiMmn2wYDz6WCVBuNQcl”, oauth_signature=“jKiLL0W6zg6pEClA%QQQQQr%2BaGHq0%3D”, oauth_version=“1.0"”

after encoding:

baseString = "POST&https%3A%2F%2Fapi.twitter.com%2Foauth%2Fauthorize&oauth_consumer_key%3DN3s5Q1jdVrjP2zKBu7eL7efAw%26oauth_nonce%3DNjM1NTU0NTQ0MTAxNDQzOTI0%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1419875610%26oauth_token%3D859829466-zavYyiWdI7MioetnmGS…

compositeKey = “UovZf9u7Zc8PgfjnSJidqRVGIWf0gUKfoqKMhr10dWbrxPLpXZ&ZicaWhq5TRVD5TQLpt67MKN4B9afMMwsRCy1MVxjlO1Ze”

oauth_signature = “2LUvMvWcYDDZ8OJSBMiceGYwCDc=”

authHeader = "OAuth oauth_nonce=“NjM1NTU0NTQ0MTAxNDQzOTI0”, oauth_signature_method=“HMAC-SHA1”, oauth_timestamp=“1419875610”, oauth_consumer_key=“N3s5Q1jdVrjP2zKBu7eL7efAw”, oauth_token=“859829466-zavYyiWdI7MioetnmGSaGiMmn2wYDz6WCVBuNQcl”, oauth_signature="2LUvMvWcYDDZ8…

postBody = “status=Hoping%20to%20be%20authorized”

IS THIS OK? I am hoping showing these values will help you help me.

Clock is in sync.
Regenerated tokens several times.
Permissions Read/Write/Access Direct msg
Not using proxy.