• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

maximepvrt/angular-google-gapi: An AngularJS module for using all Google Apis an ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

maximepvrt/angular-google-gapi

开源软件地址(OpenSource Url):

https://github.com/maximepvrt/angular-google-gapi

开源编程语言(OpenSource Language):

JavaScript 100.0%

开源软件介绍(OpenSource Introduction):

Angular Google GApi

Travis David npm Bower

An AngularJS module for using all Google Apis and your Google Cloud Endpoints (Google App Engine) with OAuth. This module uses Google APIs Client Library for JavaScript, available for all GApis.

Example

Demo

Code

Requirements

Installation

Add library

This module is available as bower package, install it with this command:

$ bower install --save angular-google-gapi

it's also available as a npm package, install it with this command:

$ npm install --save angular-google-gapi

or you may download the latest release

<script type="text/javascript" src="/angular-google-gapi/dist/angular-google-gapi.min.js"></script>

Add dependency

var app = angular.module('myModule', ['angular-google-gapi']);

Configuration

without Google Auth

add run() in root module

app.run(['GApi', 'GAuth',
    function(GApi, GAuth) {
        var BASE = 'https://myGoogleAppEngine.appspot.com/_ah/api';
        GApi.load('myApiName', 'v1', BASE).then(function(resp) {
            console.log('api: ' + resp.api + ', version: ' + resp.version + ' loaded');
        }, function(resp) {
            console.log('an error occured during loading api: ' + resp.api + ', resp.version: ' + version);
        });
    }
]);

with Google Auth

add run() in root module

app.run(['GAuth', 'GApi', 'GData', '$state', '$rootScope',
    function(GAuth, GApi, GData, $state, $rootScope) {

        $rootScope.gdata = GData;

        var CLIENT = 'yourGoogleAuthAPIKey';
        var BASE = 'https://myGoogleAppEngine.appspot.com/_ah/api';

        GApi.load('myApiName','v1',BASE);
        GApi.load('calendar','v3'); // for google api (https://developers.google.com/apis-explorer/)

        GAuth.setClient(CLIENT)
        // default scope is only https://www.googleapis.com/auth/userinfo.email
        GAuth.setScope('https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/calendar.readonly');

        // load the auth api so that it doesn't have to be loaded asynchronously
        // when the user clicks the 'login' button.
        // That would lead to popup blockers blocking the auth window
        GAuth.load();

        // or just call checkAuth, which in turn does load the oauth api.
        // if you do that, GAuth.load(); is unnecessary
        GAuth.checkAuth().then(
            function (user) {
                console.log(user.name + ' is logged in');
                $state.go('webapp.home'); // an example of action if it's possible to
                			  	        // authenticate user at startup of the application
            },
            function() {
		        $state.go('login'); // an example of action if it's impossible to
					  	          // authenticate user at startup of the application
            }
        );
    }
]);

GApi.load Error handling

GApi.load('myApiName', 'v1', BASE)
   .catch(function(api, version) {
       console.log('an error occured during loading api: ' + api + ', version: ' + version);
   });

Usage

Execute your api without params

app.controller('myController', ['$scope', 'GApi',
    function myController($scope, GApi) {
        GApi.execute('youApi', 'you.api.method.name').then(function(resp) {
            $scope.value = resp;
        }, function() {
            console.log('error :(');
        });
    }
]);

Execute your api with params

app.controller('myController', ['$scope', 'GApi',
    function myController($scope, GApi) {
        GApi.execute('youApi', 'you.api.method.name', {param: value}).then(function(resp) {
            $scope.value = resp;
        }, function() {
            console.log('error :(');
        });
    }
]);

Execute your api without params with Google Auth

app.controller('myController', ['$scope', 'GApi',
    function myController($scope, GApi) {
         GApi.executeAuth('youApi', 'you.api.method.name').then(function(resp) {
            $scope.value = resp;
        }, function() {
            console.log('error :(');
        });
    }
]);

Execute your api with params with Google Auth

app.controller('myController', ['$scope', 'GApi',
    function myController($scope, GApi) {
        GApi.executeAuth('youApi', 'you.api.method.name', {param: value}).then(function(resp) {
            $scope.value = resp;
        }, function() {
            console.log('error :(');
        });
    }
]);

Signup with Google

The login should be triggered by a user action, or you might run into issues with popup blockers. More information about this can be found in the Google APIs Client Library Documentation.

app.controller('myController', ['$scope', 'GAuth', '$state',
    function myController($scope, GAuth, $state) {
        $scope.doSignup = function() {
            GAuth.login().then(function(user) {
                console.log(user.name + ' is logged in');
                $state.go('webapp.home'); // action after the user have validated that
        				                  // your application can access their Google account
            }, function() {
                console.log('login failed');
            });
        };
    }
]);

Get user info

Get user info after login is very simple

app.controller('myController', ['$rootScope',
    function myController($rootScope) {
        console.log($rootScope.gdata.getUser().name);
    }
]);
<h1>{{gdata.getUser().name}}</h1>

User object:

  • email
  • picture (url)
  • id (Google id)
  • name (Google account name or email if don't exist)
  • link (link to Google+ page)

Development

gulp is used to minify angular-google-gapi.js (using Uglify). Execute npm install (requires Node.js and npm) to install the required packages.

Run gulp to generate a minified version (angular-google-gapi.min.js). Note that this requires gulp to be installed globally (via npm install -g gulp).




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Sponsor @lejard-h on GitHub Sponsors · GitHub发布时间:2022-06-11
下一篇:
lejard-h/google_maps_webservice发布时间:2022-06-11
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap