From e87fdb54a91af0d83819dd65e382f7efeda75505 Mon Sep 17 00:00:00 2001 From: aspiring-aster Date: Wed, 24 Jul 2024 22:35:37 -0600 Subject: [PATCH] Clean up code. V2 POST Tweet endpoint now works with Oauth v1 --- examples/example.nim | 19 ++++++++++++++----- src/xnim/v2/tweets.nim | 9 --------- xnim.nimble | 2 ++ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/examples/example.nim b/examples/example.nim index d61b81c..a6af380 100644 --- a/examples/example.nim +++ b/examples/example.nim @@ -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 diff --git a/src/xnim/v2/tweets.nim b/src/xnim/v2/tweets.nim index 2aff960..d2e54fc 100644 --- a/src/xnim/v2/tweets.nim +++ b/src/xnim/v2/tweets.nim @@ -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 = @@ -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}&"& @@ -46,7 +40,6 @@ proc OauthSignature(xAPI: XAPI, text: string, oAuthNonce: string, timeStamp: int # &"status={text}" paramString = percentEncode(paramString) - echo "paramString : " & paramString var signatureBase = outputString & paramString @@ -54,7 +47,6 @@ proc OauthSignature(xAPI: XAPI, text: string, oAuthNonce: string, timeStamp: int signatureBase = signatureBase.replace("+", "%20") signatureBase = signatureBase.replace("%7E", "~") # Don't encode ~ - echo "SignatureBase: " & signatureBase var signingKey:string = fmt"{xAPI.consumerSecret}&{xAPI.tokenSecret}" @@ -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 = %*{ diff --git a/xnim.nimble b/xnim.nimble index a7b5df6..febd74f 100644 --- a/xnim.nimble +++ b/xnim.nimble @@ -10,3 +10,5 @@ srcDir = "src" # Dependencies requires "nim >= 2.0.0" + +requires "nimcrypto >= 0.6.0" \ No newline at end of file