Skip to content

Commit

Permalink
Clean up code. V2 POST Tweet endpoint now works with Oauth v1
Browse files Browse the repository at this point in the history
  • Loading branch information
aspiring-aster committed Jul 25, 2024
1 parent b202edc commit e87fdb5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
19 changes: 14 additions & 5 deletions examples/example.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import ../src/xnim

when isMainModule:
const API_KEY: string = "APIKEY"
const OAUT_TOKEN: string = "OAUT_TOKEN"

const xCli: XAPI = newXAPI(API_KEY, OAUT_TOKEN)
let res: string = xCli.PostTextTweet("From X.nim")
echo res
# This is API Key
const CONSUMER_KEY: string = "CONSUMERKEY"

# This is the API secret key
const CONSUMER_SECRET: string = "CONSUMERSECRET"

# This is the Authentication Access Token
const ACCESS_TOKEN: string = "ACCESSTOKEN"

# This is the Authentication Access Secret
const TOKEN_SECRET: string = "TOKENSECRET"

const xCli: XAPI = newXAPI(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, TOKEN_SECRET)
let res: string = xCli.PostTextTweet("Hello from X.nim!")
echo res
9 changes: 0 additions & 9 deletions src/xnim/v2/tweets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ proc percentEncode(s: string): string =
result.add('%')
result.add(toHex(ord(c), 2))

# Example usage
echo percentEncode("Hello World!") # Output: Hello%20World%21

# Random string for oauth_nonce
proc OauthNonce(): string =
Expand All @@ -28,14 +26,10 @@ proc OauthNonce(): string =
proc OauthSignature(xAPI: XAPI, text: string, oAuthNonce: string, timeStamp: int):string =
# Follow https://developer.x.com/en/docs/authentication/oauth-1-0a/creating-a-signature
# For now, hard code POST as HTTP method
#

var outputString:string =
fmt"POST&"&
&"{percentEncode(TWEET_ENDPOINT)}&"

echo "outputString : " & outputString

var paramString:string =
fmt"oauth_consumer_key={xAPI.consumerKey}&"&
&"oauth_nonce={oAuthNonce}&"&
Expand All @@ -46,15 +40,13 @@ proc OauthSignature(xAPI: XAPI, text: string, oAuthNonce: string, timeStamp: int
# &"status={text}"

paramString = percentEncode(paramString)
echo "paramString : " & paramString

var signatureBase = outputString & paramString


signatureBase = signatureBase.replace("+", "%20")
signatureBase = signatureBase.replace("%7E", "~") # Don't encode ~

echo "SignatureBase: " & signatureBase

var signingKey:string =
fmt"{xAPI.consumerSecret}&{xAPI.tokenSecret}"
Expand All @@ -79,7 +71,6 @@ proc PostTextTweet*(xAPI: XAPI, text: string): string =
&"oauth_version=\"1.0\"," &
&"oauth_signature=\"{oauthSignature}\""

echo AUTH_STRING
client.headers = newHttpHeaders({"Content-Type": "application/json",
"authorization": AUTH_STRING})
let body = %*{
Expand Down
2 changes: 2 additions & 0 deletions xnim.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ srcDir = "src"
# Dependencies

requires "nim >= 2.0.0"

requires "nimcrypto >= 0.6.0"

0 comments on commit e87fdb5

Please sign in to comment.