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
Which ironically, puts me right back at the question in the first post. That's all I need to know is how to set the body.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
B4X:
Sub GetTwitterAccessCode(Job As HttpJob, TargetModule As Object)
   Job.Initialize("Twitter", TargetModule)
   Job.PostString("https://api.twitter.com/oauth2/token", "grant_type=client_credentials")
   Job.GetRequest.SetHeader("User-Agent", "LCARS UI")
   Job.GetRequest.SetHeader("Authorization", "Basic " & TwitterEncodedKey)
   Job.GetRequest.SetHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
   Job.GetRequest.SetHeader("Accept-Encoding", "Text/plain")
End Sub

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

This worked
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…