Commits

Kaz Nishimura committed b7a02e7

Added a new action.

Comments (0)

Files changed (3)

 
 #include <oauth_config.h>
 #include <boost/shared_ptr.hpp>
+#if _WIN32
+#include <tchar.h>
+#endif
 
 using namespace std;
 using boost::shared_ptr;
     OAuth1Authenticator1->ConsumerSecrect = _D("") CONSUMER_SECRET;
 }
 //---------------------------------------------------------------------------
+void __fastcall TMainForm::Login1Execute(TObject *Sender) {
+    shared_ptr<TCustomRESTRequest> request(new TRESTRequest(NULL));
+    request->Client = RESTClient1;
+    request->Method = rmPOST;
+    request->Resource = OAuth1Authenticator1->RequestTokenEndpoint;
 
+    OAuth1Authenticator1->CallbackEndpoint = _D("oob");
+    OAuth1Authenticator1->Authenticate(request.get());
+    request->Execute();
+    OAuth1Authenticator1->CallbackEndpoint = _D("");
+
+    if (SameText(request->Response->ContentType,
+            CONTENTTYPE_APPLICATION_X_WWW_FORM_URLENCODED)) {
+        request->Response->ContentType = CONTENTTYPE_TEXT_PLAIN;
+    }
+
+    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;
+    }
+
+    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
+}
+//---------------------------------------------------------------------------
     TabOrder = 20
     Width = 640.000000000000000000
     object SpeedButton1: TSpeedButton
+      Action = Login1
       Align = MostRight
       Height = 40.000000000000000000
-      Position.X = 576.000000000000000000
-      Text = '&Log in'
-      Width = 64.000000000000000000
+      Position.X = 560.000000000000000000
+      Width = 80.000000000000000000
     end
   end
   object StyleBook1: TStyleBook
   object ActionList1: TActionList
     Left = 32
     Top = 96
+    object Login1: TAction
+      Text = '&Log in'
+      OnExecute = Login1Execute
+    end
   end
   object RESTClient1: TRESTClient
     Authenticator = OAuth1Authenticator1
     TRESTResponse *RESTResponse1;
     TOAuth1Authenticator *OAuth1Authenticator1;
 
+    TAction *Login1;
+
     TToolBar *ToolBar1;
     TSpeedButton *SpeedButton1;
 
+    void __fastcall Login1Execute(TObject *Sender);
+
 public:
     __fastcall TMainForm(TComponent *Owner);
 };