1. Kaz Nishimura
  2. bitbucket-api-demo

Commits

Kaz Nishimura  committed dff4655

Implemented code to get an access token and its secret.

  • Participants
  • Parent commits b7a02e7
  • Branches default

Comments (0)

Files changed (2)

File src/TMainForm.cpp

View file
  • Ignore whitespace
 #include <oauth_config.h>
 #include <boost/shared_ptr.hpp>
 #if _WIN32
+#include <FMX.Platform.Win.hpp>
+#include <FMX.Dialogs.hpp>
+#endif
+
+#if _WIN32
 #include <tchar.h>
 #endif
 
 using namespace std;
 using boost::shared_ptr;
 
+namespace {
+    template<typename Function>
+    class TProcWrapper : public TInterfacedObject, public TProc {
+    public:
+        __fastcall TProcWrapper(Function Proc)
+                : FProc(Proc) {
+        }
+
+        virtual __fastcall ~TProcWrapper() {
+        }
+
+        virtual void __fastcall Invoke() {
+            FProc();
+        }
+
+        INTFOBJECT_IMPL_IUNKNOWN(TInterfacedObject);
+
+    private:
+        Function FProc;
+    };
+
+    template<typename Function>
+    _di_TProc CreateProc(Function Proc) {
+        return new TProcWrapper<Function>(Proc);
+    }
+}
+
 //---------------------------------------------------------------------------
 #pragma package(smart_init)
 #pragma resource "*.fmx"
 }
 //---------------------------------------------------------------------------
 void __fastcall TMainForm::Login1Execute(TObject *Sender) {
-    shared_ptr<TCustomRESTRequest> request(new TRESTRequest(NULL));
-    request->Client = RESTClient1;
-    request->Method = rmPOST;
-    request->Resource = OAuth1Authenticator1->RequestTokenEndpoint;
+    Login1->Enabled = false;
+
+    OAuth1Authenticator1->AccessToken = _D("");
+    OAuth1Authenticator1->AccessTokenSecret = _D("");
+
+    RESTRequest1->ResetToDefaults();
+    RESTRequest1->Method = rmPOST;
+    RESTRequest1->Resource = OAuth1Authenticator1->RequestTokenEndpoint;
 
     OAuth1Authenticator1->CallbackEndpoint = _D("oob");
-    OAuth1Authenticator1->Authenticate(request.get());
-    request->Execute();
+    RESTRequest1->ExecuteAsync(CreateProc(&Authenticate));
+}
+//---------------------------------------------------------------------------
+
+void __fastcall TMainForm::Authenticate() {
     OAuth1Authenticator1->CallbackEndpoint = _D("");
 
-    if (SameText(request->Response->ContentType,
+    TCustomRESTResponse *response = RESTRequest1->Response;
+    UnicodeString token, tokenSecret;
+    // Note: the REST client library dislikes content type
+    // 'application/x-www-form-urlencoded'.
+    if (SameText(response->ContentType,
             CONTENTTYPE_APPLICATION_X_WWW_FORM_URLENCODED)) {
-        request->Response->ContentType = CONTENTTYPE_TEXT_PLAIN;
+        response->ContentType = CONTENTTYPE_TEXT_PLAIN;
+    }
+    if (response->GetSimpleValue(_D("oauth_token"),
+            token) && response->GetSimpleValue(_D("oauth_token_secret"),
+            tokenSecret)) {
+        /* Note: we must set the request token and its secet as the access
+           token and its secret somehow to generate a signature right. */
+        OAuth1Authenticator1->AccessToken = token;
+        OAuth1Authenticator1->AccessTokenSecret = tokenSecret;
+
+        try {
+            RESTRequest1->ResetToDefaults();
+            RESTRequest1->Resource =
+                    OAuth1Authenticator1->AuthenticationEndpoint;
+            RESTRequest1->AddParameter(_D("oauth_token"),
+                    OAuth1Authenticator1->AccessToken);
+
+#if _WIN32
+            UnicodeString location = RESTRequest1->GetFullRequestURL();
+            SHELLEXECUTEINFO info = {};
+            info.cbSize = sizeof(SHELLEXECUTEINFO);
+            info.hwnd = FormToHWND(this);
+            info.lpVerb = _T("open");
+            info.lpFile = location.c_str();
+            info.nShow = SW_SHOWNORMAL;
+            ShellExecuteEx(&info);
+#endif
+
+            UnicodeString verifier =
+                    InputBox(_D("Verification"), _D("&Verification code:"),
+                    _D("")).Trim();
+            if (!verifier.IsEmpty()) {
+                OAuth1Authenticator1->VerifierPIN = verifier;
+
+                RESTRequest1->ResetToDefaults();
+                RESTRequest1->Method = rmPOST;
+                RESTRequest1->Resource =
+                        OAuth1Authenticator1->AccessTokenEndpoint;
+                RESTRequest1->AddParameter(_D("oauth_verifier"),
+                        OAuth1Authenticator1->VerifierPIN);
+                RESTRequest1->Execute();
+
+                // Note: the REST client library dislikes content type
+                // 'application/x-www-form-urlencoded'.
+                if (SameText(response->ContentType,
+                        CONTENTTYPE_APPLICATION_X_WWW_FORM_URLENCODED)) {
+                    response->ContentType = CONTENTTYPE_TEXT_PLAIN;
+                }
+                if (response->GetSimpleValue(_D("oauth_token"),
+                        token) && response->GetSimpleValue
+                        (_D("oauth_token_secret"), tokenSecret)) {
+                    OAuth1Authenticator1->AccessToken = token;
+                    OAuth1Authenticator1->AccessTokenSecret = tokenSecret;
+
+                    return;
+                }
+            }
+        } catch (...) {
+            // Resets the access token and token secret.
+            OAuth1Authenticator1->AccessToken = _D("");
+            OAuth1Authenticator1->AccessTokenSecret = _D("");
+
+            Login1->Enabled = true;
+            throw;
+        }
     }
 
-    UnicodeString value;
-    if (request->Response->GetSimpleValue(_D("oauth_token"), value)) {
-        OAuth1Authenticator1->RequestToken = value;
-    }
-    if (request->Response->GetSimpleValue(_D("oauth_token_secret"), value)) {
-        OAuth1Authenticator1->RequestTokenSecret = value;
-    }
+    // Resets the access token and token secret.
+    OAuth1Authenticator1->AccessToken = _D("");
+    OAuth1Authenticator1->AccessTokenSecret = _D("");
 
-    request->ResetToDefaults();
-    request->Resource = OAuth1Authenticator1->AuthenticationEndpoint;
-    request->AddParameter(_D("oauth_token"),
-            OAuth1Authenticator1->RequestToken);
-
-#if _WIN32
-    UnicodeString location = request->GetFullRequestURL(true);
-    SHELLEXECUTEINFO info = {};
-    info.cbSize = sizeof (SHELLEXECUTEINFO);
-    info.lpVerb = _T("open");
-    info.lpFile = location.c_str();
-    info.nShow = SW_SHOWNORMAL;
-    ShellExecuteEx(&info);
-#endif
+    Login1->Enabled = true;
 }
-//---------------------------------------------------------------------------

File src/TMainForm.h

View file
  • Ignore whitespace
 
 public:
     __fastcall TMainForm(TComponent *Owner);
+
+protected:
+    void __fastcall Authenticate();
 };
 //---------------------------------------------------------------------------
 extern PACKAGE TMainForm *MainForm;