{"id":263,"date":"2014-11-07T02:00:00","date_gmt":"2014-11-07T02:00:00","guid":{"rendered":"http:\/\/localhost\/?p=263"},"modified":"2016-11-13T20:45:01","modified_gmt":"2016-11-13T20:45:01","slug":"oauth-ionic-ngcordova","status":"publish","type":"post","link":"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova","title":{"rendered":"OAuth with Ionic and ngCordova"},"content":{"rendered":"<p><em>This is a guest post by Nic Raboy, an application developer with a strong background in Android, AngularJS, Ionic, Java, SQL, and Unity3D. Nic writes often about Ionic and how to build great hybrid apps.<\/em><\/p>\n<p>I get a lot of inquiries regarding OAuth 2.0 with Apache Cordova and Ionic Framework applications. Although I do have <a href=\"http:\/\/blog.nraboy.com\/2014\/07\/using-oauth-2-0-service-ionicframework\/\" rel=\"nofollow\">another post<\/a> elaborating on how to accomplish this, I decided to take it a step further and make a simplistic library for AngularJS.<\/p>\n<p>I created <a href=\"https:\/\/github.com\/nraboy\/ng-cordova-oauth\" rel=\"nofollow\">ngCordovaOauth<\/a>, which can be found on GitHub. It was designed to make it as simple as possible to obtain access tokens for some of the popular web APIs, such as Google.<\/p>\n<p><!--more--><\/p>\n<p>I took my ngCordovaOauth library a step further, and <a href=\"http:\/\/ngcordova.com\/docs\/#Oauth\" rel=\"nofollow\">included it<\/a> in the official <a href=\"http:\/\/ngcordova.com\/\" rel=\"nofollow\">ngCordova<\/a> project. For the sake of this tutorial, I&#8217;m going to explain things around the official ngCordova library which is now the official implementation.<\/p>\n<p>When I was designing the OAuth functionality, my strategy was to expect a promise based on the login flow. If the flow is successful, we will receive an object containing whatever the server responds with. Otherwise, we&#8217;ll get some kind of error.<\/p>\n<p>Although I designed this with Ionic Framework in mind, it will also work for any Apache Cordova or Phonegap application that makes use of AngularJS. With this in mind, let&#8217;s set up the library for use.<\/p>\n<pre><code class=\"bash\">ionic start IonicProject blank\ncd IonicProject\nionic platform add android\nionic platform add ios\n<\/code><\/pre>\n<p>Note that if you are not using a Mac, you cannot build for iOS.<\/p>\n<p>OAuth with ngCordova relies heavily on the Apache Cordova <a href=\"http:\/\/cordova.apache.org\/docs\/en\/3.0.0\/cordova_inappbrowser_inappbrowser.md.html\" rel=\"nofollow\">InAppBrowser<\/a> plugin. If you&#8217;d like to know more about how this plugin works, you can view my <a href=\"http:\/\/blog.nraboy.com\/2014\/07\/launch-external-urls-ionicframework\/\" rel=\"nofollow\">previous post<\/a> on the topic, otherwise just continue by running the following:<\/p>\n<pre><code class=\"bash\">cordova plugin add https:\/\/git-wip-us.apache.org\/repos\/asf\/cordova-plugin-inappbrowser.git\n<\/code><\/pre>\n<p>Your Ionic project is now ready for the ngCordova library.  <a href=\"https:\/\/github.com\/driftyco\/ng-cordova\/archive\/master.zip\" rel=\"nofollow\">Download<\/a> it and include <strong>ng-cordova.min.js<\/strong> into your <strong>www\/js<\/strong> directory.<\/p>\n<p>Open the <strong>www\/index.html<\/strong> file and include the following:<\/p>\n<pre><code class=\"html\">&lt;script src=&quot;js\/ng-cordova.min.js&quot;&gt;&lt;\/script&gt;\n<\/code><\/pre>\n<p>One more item must be added in order to fully make use of this plugin. You must inject ngCordova into your AngularJS module found in the <strong>www\/js\/app.js<\/strong> file:<\/p>\n<pre><code class=\"javascript\">angular.module(&#039;starter&#039;, [&#039;ionic&#039;, &#039;ngCordova&#039;])\n<\/code><\/pre>\n<p>Here is an example of how you might use the OAuth with the ngCordova library:<\/p>\n<pre><code class=\"javascript\">var ionicExample = angular.module(&#039;starter&#039;, [&#039;ionic&#039;, &#039;ngCordova&#039;]);\n\nionicExample.controller(&quot;OauthExample&quot;, function($scope, $cordovaOauth) {\n\n    $scope.googleLogin = function() {\n        $cordovaOauth.google(&quot;CLIENT_ID_HERE&quot;, [&quot;https:\/\/www.googleapis.com\/auth\/urlshortener&quot;, &quot;https:\/\/www.googleapis.com\/auth\/userinfo.email&quot;]).then(function(result) {\n            console.log(JSON.stringify(result));\n        }, function(error) {\n            console.log(error);\n        });\n    }\n\n});\n<\/code><\/pre>\n<p>As of right now, the following methods are available:<\/p>\n<pre><code class=\"bash\">$cordovaOauth.dropbox(string appKey);\n$cordovaOauth.digitalOcean(string clientId, string clientSecret);\n$cordovaOauth.google(string clientId, array appScope);\n$cordovaOauth.github(string clientId, string clientSecret, array appScope);\n$cordovaOauth.facebook(string clientId, array appScope);\n$cordovaOauth.linkedin(string clientId, string clientSecret, array appScope, string state);\n$cordovaOauth.instagram(string clientId, array appScope);\n$cordovaOauth.box(string clientId, string clientSecret, string state);\n<\/code><\/pre>\n<p>There are some things to note about OAuth with ngCordova:<\/p>\n<ul>\n<li>It will only work on your device or simulator. It will not work in the web browser.<\/li>\n<li>All redirect \/ callback URIs must point to http:\/\/localhost\/callback; otherwise, the login flow will not complete.<\/li>\n<\/ul>\n<p>When you have your access token, you can typically make full use of the web APIs for that service. For example, if you wanted to use one of the Digital Ocean APIs, you might do the following:<\/p>\n<pre><code class=\"javascript\">var ionicExample = angular.module(&#039;starter&#039;, [&#039;ionic&#039;, &#039;ngCordova&#039;]);\n\nionicExample.controller(&quot;DigitalOceanExample&quot;, function($scope, $http, $cordovaOauth) {\n\n    $scope.digitalOceanLogin = function() {\n        $cordovaOauth.digitalOcean(&quot;CLIENT_ID_HERE&quot;, &quot;CLIENT_SECRET_HERE&quot;).then(function(result) {\n            window.localStorage.setItem(&quot;access_token&quot;, result.access_token);\n        }, function(error) {\n            console.log(error);\n        });\n    }\n\n    $scope.getDroplets = function() {\n        $http.defaults.headers.common.Authorization = &quot;Bearer &quot; + window.localStorage.getItem(&quot;access_token&quot;);\n        $http.get(&quot;https:\/\/api.digitalocean.com\/v2\/droplets&quot;)\n            .success(function(data) {\n                console.log(JSON.stringify(data.droplets));\n            })\n            .error(function(error) {\n                console.log(error);\n            });\n    }\n\n});\n<\/code><\/pre>\n<p>Notice that in the above example, we are using the access token. In this particular example, the access token is used in the header, but in many APIs, the access token is passed in the POST or GET parameters.<\/p>\n<h3 id=\"conclusion\">Conclusion<\/h3>\n<p>Adding OAuth to your Ionic apps is easy with the new <a href=\"http:\/\/ngcordova.com\/docs\/#Oauth\" rel=\"nofollow\">OAuth support<\/a> in ngCordova. With support for such providers as Google, Facebook, and Dropbox, it&#8217;s just a matter of adding your authentication tokens and linking with the plugin. <\/p>\n<p>Support for Twitter, which uses a slightly different client-side token system, is coming soon!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a guest post by Nic Raboy, an application developer with a strong background in Android, AngularJS, Ionic, Java, SQL, and Unity3D. Nic writes often about Ionic and how to build great hybrid apps. I get a lot of inquiries regarding OAuth 2.0 with Apache Cordova and Ionic Framework applications. Although I do have [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"publish_to_discourse":"","publish_post_category":"","wpdc_auto_publish_overridden":"","wpdc_topic_tags":"","wpdc_pin_topic":"","wpdc_pin_until":"","discourse_post_id":"","discourse_permalink":"","wpdc_publishing_response":"","wpdc_publishing_error":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[3],"class_list":["post-263","post","type-post","status-publish","format-standard","hentry","category-all","tag-ionic"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v23.0 (Yoast SEO v23.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>OAuth with Ionic and ngCordova - Ionic Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"OAuth with Ionic and ngCordova\" \/>\n<meta property=\"og:description\" content=\"This is a guest post by Nic Raboy, an application developer with a strong background in Android, AngularJS, Ionic, Java, SQL, and Unity3D. Nic writes often about Ionic and how to build great hybrid apps. I get a lot of inquiries regarding OAuth 2.0 with Apache Cordova and Ionic Framework applications. Although I do have [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova\" \/>\n<meta property=\"og:site_name\" content=\"Ionic Blog\" \/>\n<meta property=\"article:published_time\" content=\"2014-11-07T02:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-11-13T20:45:01+00:00\" \/>\n<meta name=\"author\" content=\"Nic Raboy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@nraboy\" \/>\n<meta name=\"twitter:site\" content=\"@ionicframework\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nic Raboy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova#article\",\"isPartOf\":{\"@id\":\"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova\"},\"author\":{\"name\":\"Nic Raboy\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/person\/e4c6a16b5b7a115afcdab9816c3f9e77\"},\"headline\":\"OAuth with Ionic and ngCordova\",\"datePublished\":\"2014-11-07T02:00:00+00:00\",\"dateModified\":\"2016-11-13T20:45:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova\"},\"wordCount\":550,\"publisher\":{\"@id\":\"https:\/\/ionic.io\/blog\/#organization\"},\"keywords\":[\"Ionic\"],\"articleSection\":[\"All\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova\",\"url\":\"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova\",\"name\":\"OAuth with Ionic and ngCordova - Ionic Blog\",\"isPartOf\":{\"@id\":\"https:\/\/ionic.io\/blog\/#website\"},\"datePublished\":\"2014-11-07T02:00:00+00:00\",\"dateModified\":\"2016-11-13T20:45:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ionic.io\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"OAuth with Ionic and ngCordova\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ionic.io\/blog\/#website\",\"url\":\"https:\/\/ionic.io\/blog\/\",\"name\":\"ionic.io\/blog\",\"description\":\"Build amazing native and progressive web apps with the web\",\"publisher\":{\"@id\":\"https:\/\/ionic.io\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ionic.io\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/ionic.io\/blog\/#organization\",\"name\":\"Ionic\",\"url\":\"https:\/\/ionic.io\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png\",\"contentUrl\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png\",\"width\":1920,\"height\":854,\"caption\":\"Ionic\"},\"image\":{\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/ionicframework\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/person\/e4c6a16b5b7a115afcdab9816c3f9e77\",\"name\":\"Nic Raboy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/80ca16a986c827c91604fac0c8eba63e2eae09be264ec1f701cf75633451daa2?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/80ca16a986c827c91604fac0c8eba63e2eae09be264ec1f701cf75633451daa2?s=96&d=mm&r=g\",\"caption\":\"Nic Raboy\"},\"description\":\"This is a guest post by Nic Raboy, an application developer with a strong background in Android, AngularJS, Ionic, Java, SQL, and Unity3D. Nic writes often about Ionic and how to build great hybrid apps. He can be further followed via his blog at https:\/\/www.thepolyglotdeveloper.com\",\"sameAs\":[\"https:\/\/www.thepolyglotdeveloper.com\",\"https:\/\/x.com\/nraboy\"],\"url\":\"https:\/\/ionic.io\/blog\/author\/nraboy\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"OAuth with Ionic and ngCordova - Ionic Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova","og_locale":"en_US","og_type":"article","og_title":"OAuth with Ionic and ngCordova","og_description":"This is a guest post by Nic Raboy, an application developer with a strong background in Android, AngularJS, Ionic, Java, SQL, and Unity3D. Nic writes often about Ionic and how to build great hybrid apps. I get a lot of inquiries regarding OAuth 2.0 with Apache Cordova and Ionic Framework applications. Although I do have [&hellip;]","og_url":"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova","og_site_name":"Ionic Blog","article_published_time":"2014-11-07T02:00:00+00:00","article_modified_time":"2016-11-13T20:45:01+00:00","author":"Nic Raboy","twitter_card":"summary_large_image","twitter_creator":"@nraboy","twitter_site":"@ionicframework","twitter_misc":{"Written by":"Nic Raboy","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova#article","isPartOf":{"@id":"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova"},"author":{"name":"Nic Raboy","@id":"https:\/\/ionic.io\/blog\/#\/schema\/person\/e4c6a16b5b7a115afcdab9816c3f9e77"},"headline":"OAuth with Ionic and ngCordova","datePublished":"2014-11-07T02:00:00+00:00","dateModified":"2016-11-13T20:45:01+00:00","mainEntityOfPage":{"@id":"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova"},"wordCount":550,"publisher":{"@id":"https:\/\/ionic.io\/blog\/#organization"},"keywords":["Ionic"],"articleSection":["All"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova","url":"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova","name":"OAuth with Ionic and ngCordova - Ionic Blog","isPartOf":{"@id":"https:\/\/ionic.io\/blog\/#website"},"datePublished":"2014-11-07T02:00:00+00:00","dateModified":"2016-11-13T20:45:01+00:00","breadcrumb":{"@id":"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/ionic.io\/blog\/oauth-ionic-ngcordova#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ionic.io\/blog"},{"@type":"ListItem","position":2,"name":"OAuth with Ionic and ngCordova"}]},{"@type":"WebSite","@id":"https:\/\/ionic.io\/blog\/#website","url":"https:\/\/ionic.io\/blog\/","name":"ionic.io\/blog","description":"Build amazing native and progressive web apps with the web","publisher":{"@id":"https:\/\/ionic.io\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ionic.io\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ionic.io\/blog\/#organization","name":"Ionic","url":"https:\/\/ionic.io\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png","contentUrl":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png","width":1920,"height":854,"caption":"Ionic"},"image":{"@id":"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/ionicframework"]},{"@type":"Person","@id":"https:\/\/ionic.io\/blog\/#\/schema\/person\/e4c6a16b5b7a115afcdab9816c3f9e77","name":"Nic Raboy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ionic.io\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/80ca16a986c827c91604fac0c8eba63e2eae09be264ec1f701cf75633451daa2?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/80ca16a986c827c91604fac0c8eba63e2eae09be264ec1f701cf75633451daa2?s=96&d=mm&r=g","caption":"Nic Raboy"},"description":"This is a guest post by Nic Raboy, an application developer with a strong background in Android, AngularJS, Ionic, Java, SQL, and Unity3D. Nic writes often about Ionic and how to build great hybrid apps. He can be further followed via his blog at https:\/\/www.thepolyglotdeveloper.com","sameAs":["https:\/\/www.thepolyglotdeveloper.com","https:\/\/x.com\/nraboy"],"url":"https:\/\/ionic.io\/blog\/author\/nraboy"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/posts\/263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/comments?post=263"}],"version-history":[{"count":0,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"wp:attachment":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}