[vlmc-devel] [PATCH 5/6] Added GoogleDrive.

Paweł Wegner pawel.wegner95 at gmail.com
Mon May 9 15:40:04 CEST 2016


---
 src/GoogleDrive.cpp | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/GoogleDrive.h   |  51 +++++++++++++++++++
 src/Makefile.am     |   3 +-
 test/main.cpp       |   3 +-
 4 files changed, 194 insertions(+), 2 deletions(-)
 create mode 100644 src/GoogleDrive.cpp
 create mode 100644 src/GoogleDrive.h

diff --git a/src/GoogleDrive.cpp b/src/GoogleDrive.cpp
new file mode 100644
index 0000000..60aec89
--- /dev/null
+++ b/src/GoogleDrive.cpp
@@ -0,0 +1,139 @@
+/*****************************************************************************
+ * GoogleDrive.cpp : implementation of GoogleDrive
+ *                   
+ *****************************************************************************
+ * Copyright (C) 2016-2016 VideoLAN
+ *
+ * Authors: Paweł Wegner <pawel.wegner95 at gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#include "GoogleDrive.h"
+
+#include <jsoncpp/json/json.h>
+#include <cstring>
+#include <curlpp/Easy.hpp>
+#include <curlpp/Options.hpp>
+#include <iostream>
+#include <sstream>
+
+using namespace curlpp::options;
+
+namespace cloudstorage {
+
+GoogleDrive::GoogleDrive() {
+  set_client_id(
+      "646432077068-hmvk44qgo6d0a64a5h9ieue34p3j2dcv.apps.googleusercontent."
+      "com");
+  set_client_secret("1f0FG5ch-kKOanTAv1Bqdp9U");
+}
+
+void GoogleDrive::set_client_secret(const std::string& client_secret) {
+  client_secret_ = client_secret;
+}
+
+const std::string& GoogleDrive::client_secret() const { return client_secret_; }
+
+std::string GoogleDrive::authorizeLibraryUrl() const {
+  std::string response_type = "code";
+  std::string scope = "https://www.googleapis.com/auth/drive";
+  std::string url = "https://accounts.google.com/o/oauth2/auth?";
+  addParameter(url, "response_type", response_type);
+  addParameter(url, "client_id", client_id());
+  addParameter(url, "redirect_uri", redirect_uri());
+  addParameter(url, "scope", scope);
+  return url;
+}
+
+// TODO
+std::vector<ICloudStorage::File> GoogleDrive::listDirectory(
+    const std::string&) const {
+  return {};
+}
+
+std::unique_ptr<ICloudStorage::Token> GoogleDrive::requestAccessToken() const {
+  curlpp::Easy request;
+  std::stringstream stream;
+  request.setOpt<Url>("https://accounts.google.com/o/oauth2/token");
+  request.setOpt(curlpp::options::WriteStream(&stream));
+  std::string post_data;
+  addParameter(post_data, "grant_type", "authorization_code");
+  addParameter(post_data, "client_secret", client_secret());
+  addParameter(post_data, "code", authorization_code());
+  addParameter(post_data, "client_id", client_id());
+  addParameter(post_data, "redirect_uri", redirect_uri());
+  request.setOpt(curlpp::options::PostFields(post_data));
+  request.perform();
+
+  Json::Value response;
+  stream >> response;
+
+  if (response.isMember("access_token")) {
+    std::unique_ptr<Token> token(new Token(
+        response["access_token"].asString(), response["expires_in"].asInt(),
+        response["refresh_token"].asString()));
+    return token;
+  }
+
+  return nullptr;
+}
+
+std::unique_ptr<ICloudStorage::Token> GoogleDrive::refreshToken() const {
+  if (!access_token()) return nullptr;
+
+  curlpp::Easy request;
+  std::stringstream stream;
+  request.setOpt(curlpp::options::WriteStream(&stream));
+  request.setOpt<Url>("https://accounts.google.com/o/oauth2/token");
+  std::string post_data;
+  addParameter(post_data, "refresh_token", access_token()->refresh_token());
+  addParameter(post_data, "client_id", client_id());
+  addParameter(post_data, "client_secret", client_secret());
+  addParameter(post_data, "grant_type", "refresh_token");
+  request.setOpt(curlpp::options::PostFields(post_data));
+  request.perform();
+  Json::Value response;
+  stream >> response;
+  if (response.isMember("access_token")) {
+    std::unique_ptr<Token> token(new Token(response["access_token"].asString(),
+                                           response["expires_in"].asInt(),
+                                           access_token()->refresh_token()));
+    return token;
+  }
+
+  return nullptr;
+}
+
+bool GoogleDrive::validateToken(ICloudStorage::Token* token) const {
+  curlpp::Easy request;
+  std::stringstream stream;
+  request.setOpt(curlpp::options::WriteStream(&stream));
+  std::string url = "https://www.googleapis.com/oauth2/v3/tokeninfo?";
+  addParameter(url, "access_token", token->token());
+  request.setOpt<Url>(url);
+  request.perform();
+
+  Json::Value response;
+  stream >> response;
+
+  if (response.isMember("expires_in")) {
+    token->set_expires_in(atoi(response["expires_in"].asCString()));
+    return true;
+  } else
+    return false;
+}
+
+}  // namespace cloudstorage
diff --git a/src/GoogleDrive.h b/src/GoogleDrive.h
new file mode 100644
index 0000000..088a4fc
--- /dev/null
+++ b/src/GoogleDrive.h
@@ -0,0 +1,51 @@
+/*****************************************************************************
+ * GoogleDrive.h : prototypes for GoogleDrive
+ *                   
+ *****************************************************************************
+ * Copyright (C) 2016-2016 VideoLAN
+ *
+ * Authors: Paweł Wegner <pawel.wegner95 at gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifndef GOOGLEDRIVE_H
+#define GOOGLEDRIVE_H
+
+#include "ICloudStorage.h"
+
+namespace cloudstorage {
+
+class GoogleDrive : public ICloudStorage {
+ public:
+  GoogleDrive();
+
+  void set_client_secret(const std::string&);
+  const std::string& client_secret() const;
+
+  std::string authorizeLibraryUrl() const;
+  std::vector<File> listDirectory(const std::string& path) const;
+
+  std::unique_ptr<Token> requestAccessToken() const;
+  std::unique_ptr<Token> refreshToken() const;
+  bool validateToken(Token*) const;
+
+ private:
+  std::string client_secret_;
+};
+
+}  // namespace cloudstorage
+
+#endif  // GOOGLEDRIVE_H
diff --git a/src/Makefile.am b/src/Makefile.am
index 45f2256..3dfc17a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -7,7 +7,8 @@ AM_CXXFLAGS = \
 
 lib_LTLIBRARIES = libcloudstorage.la
 libcloudstorage_la_SOURCES = \
-	ICloudStorage.cpp ICloudStorage.h
+	ICloudStorage.cpp ICloudStorage.h \
+	GoogleDrive.cpp GoogleDrive.h
 
 libcloudstorage_la_LIBADD = \
 	$(libcurlpp_LIBS) \
diff --git a/test/main.cpp b/test/main.cpp
index accdc73..c31ac52 100644
--- a/test/main.cpp
+++ b/test/main.cpp
@@ -1,5 +1,6 @@
-#include <ICloudStorage.h>
+#include <GoogleDrive.h>
 
 int main() {
+  cloudstorage::GoogleDrive gdrive;
   return 0;
 }
-- 
2.7.4



More information about the Vlmc-devel mailing list