Android Question How do I form this HTTPutils request?

NeoTechni

Well-Known Member
Licensed User
Longtime User
Obtain a bearer token
The value calculated in step 1 must be exchanged for a bearer token by issuing a request to POST oauth2/token:

  • The request must be a HTTP POST request.
  • The request must include an Authorization header with the value of Basic <base64 encoded value from step 1>.
  • The request must include a Content-Type header with the value of application/x-www-form-urlencoded;charset=UTF-8.
  • The body of the request must be grant_type=client_credentials.
Example request (Authorization header has been wrapped):

POST /oauth2/token HTTP/1.1
Host: api.twitter.com
User-Agent: My Twitter App v1.0.23
Authorization: Basic eHZ6MWV2RlM0d0VFUFRHRUZQSEJvZzpMOHFxOVBaeVJn
NmllS0dFS2hab2xHQzB2SldMdzhpRUo4OERSZHlPZw==
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
Content-Length: 29
Accept-Encoding: gzip

grant_type=client_credentials


I have most of that done, it's just the "body of the request" I don't know how to do



B4X:
Sub EncodeTwitterConsumerKeyAndSecret(Key As String, Secret As String)
    Dim STRutils As StringUtils , BYTutils As ByteConverter ,tempstr As String
    tempstr = STRutils.EncodeUrl(Key, "UTF8") & ":" & STRutils.EncodeUrl(Secret, "UTF8")
    TwitterEncodedKey = STRutils.EncodeBase64( BYTutils.StringToBytes(tempstr, "UTF8"))
End Sub

'What I have so far:
HttpUtils.PostString("Twitter", "https://api.twitter.com/oauth2/token", "Authorization=Basic " & TwitterEncodedKey & "&Content-Type=application/x-www-form-urlencoded;charset=UTF-8")
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Cause I was using HTTPutils and didnt want to have to change any existing code.

Job.GetRequest.SetHeader will let me set the body of the request to grant_type=client_credentials?
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
It says "The body of the request must be grant_type=client_credentials." though. I don't know how to do that.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Why are you using HttpUtils instead of HttpUtils2?

Immediately after sending the request you should call Job.GetRequest.SetHeader to set the two headers.

I'm not seeing those methods either.
And I'm trying the request using wfetch, and it needs "grant_type=client_credentials" separated by an empty line from the other post values.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Another problem has arisen. Since the Base64 encoded text will end in an = sign, won't it mess up the rest of the POST data?
Should encodeURL deal with it?
 
Upvote 0

gfichter

New Member
Licensed User
Longtime User
This is my exact problem. How do you set the content type using the HttpUtils2 library? I need to set my content type to application/xml
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I don't know why twitter went out of their way to make this a pain in the ass.
If "grant_type=client_credentials" was a post value rather than the body, I could do it. (Or not required at all...)
If it used HTTP instead of HTTPS, I could do it.
URL encoding the Base64 string doesn't appear to work.
But combined I have no idea how. I have the SocketSSL library, but I can't find documentation on it.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I meant i was going to telnet in and give the raw data since i cant get httputils to give me access to a post requests body
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
B4X:
Sub GetTwitterAccessCode(Job As HttpJob, TargetModule As Object)
    'Dim SSL As SocketSSL
    'SSL.Initialize("SSL")
    'SSL.Connect("api.twitter.com", 443, 20)
    Dim tempstr As StringBuilder , BYTutils As ByteConverter
    tempstr.Initialize
    tempstr.Append("User-Agent: LCARS UI\r\n")
    tempstr.Append("Authorization: Basic " & TwitterEncodedKey & "\r\n")
    tempstr.Append("Content-Type: application/x-www-form-urlencoded;charset=UTF-8\r\n")
    tempstr.Append("Content-Length: 29\r\n")
    tempstr.Append("Accept-Encoding: gzip\r\n")
    tempstr.Append("\r\n")
    tempstr.Append("grant_type=client_credentials")
  
    Job.Initialize("Twitter", TargetModule)
    Job.GetRequest.InitializePost2("https://api.twitter.com/oauth2/token", BYTutils.StringToBytes(tempstr.ToString, "UTF8") )
End Sub

I am lost at this point...

I also tried:

B4X:
Sub GetTwitterAccessCode(Job As HttpJob, TargetModule As Object)
    'Dim SSL As SocketSSL
    'SSL.Initialize("SSL")
    'SSL.Connect("api.twitter.com", 443, 20)
    Dim tempstr As StringBuilder , BYTutils As ByteConverter
    tempstr.Initialize
    tempstr.Append("User-Agent: LCARS UI" & CRLF)
    tempstr.Append("Authorization: Basic " & TwitterEncodedKey & CRLF)
    tempstr.Append("Content-Type: application/x-www-form-urlencoded;charset=UTF-8" & CRLF)
    tempstr.Append("Content-Length: 29" & CRLF)
    tempstr.Append("Accept-Encoding: text/plain" & CRLF)
    tempstr.Append(CRLF)
    tempstr.Append("grant_type=client_credentials")
   
    Job.Initialize("Twitter", TargetModule)
    Job.PostString("https://api.twitter.com/oauth2/token", "User-Agent=LCARS UI")
    Log(tempstr.ToString)
   
    Job.GetRequest.InitializePost2("https://api.twitter.com/oauth2/token", BYTutils.StringToBytes(tempstr.ToString, "UTF8") )
End Sub
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…