From 2e3fbfebbf3a62364cfca58f9b46e425dbf0e812 Mon Sep 17 00:00:00 2001 From: Jinyoung Kim Date: Sun, 15 Nov 2015 17:10:07 +0900 Subject: [PATCH 1/2] fix(credential): not supported api-key way The API supports two types of credentials, which are OAuth 2.0 and API keys. These ways are exclusive for each other. So, if `clientId` is filled in `GoogleApp` whether with or without `apiKey`, `apiKey` will not be used (API works with OAuth flow). But, only `apiKey` is filled in `GoogleApp`, OAuth flow will not be worked (API works with public API key). --- gapi.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/gapi.js b/gapi.js index 3170b9c..c220687 100644 --- a/gapi.js +++ b/gapi.js @@ -105,9 +105,21 @@ angular.module('gapi', []) /** - * OAuth 2.0 Signatures + * Credential exclusive way: API-key or Oauth 2.0 */ + function credential(options) { + if (GAPI.app.apiKey && !GAPI.app.clientId) { + setApiKey(options); + } else { + oauthHeader(options); + } + } + + function setApiKey(options) { + options.params.key = GAPI.app.apiKey; + } + function oauthHeader(options) { if (!options.headers) { options.headers = {}; } options.headers['Authorization'] = 'Bearer ' + GAPI.app.oauthToken.access_token; @@ -126,7 +138,7 @@ angular.module('gapi', []) function request (config) { var deferred = $q.defer(); - oauthHeader(config); + credential(config); function success(response) { $log.log('Request success: ', config, response); From e42baedb5e9d4781250875f798f8270f7690eb95 Mon Sep 17 00:00:00 2001 From: Jinyoung Kim Date: Sun, 15 Nov 2015 18:05:53 +0900 Subject: [PATCH 2/2] docs(readme): update usage --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4170beb..66cb6f2 100644 --- a/README.md +++ b/README.md @@ -33,9 +33,9 @@ Be sure to include "gapi" as a dependency in your main app module. After you register your app in the [Google APIs Console](https://code.google.com/apis/console), configure ngGAPI with credentials and whatever scopes you need for your app. +###with OAuth 2.0 angular.module('myApp') .value('GoogleApp', { - apiKey: 'YOUR_API_KEY', clientId: 'YOUR_CLIENT_ID', scopes: [ // whatever scopes you need for your app, for example: @@ -44,7 +44,13 @@ After you register your app in the [Google APIs Console](https://code.google.com 'https://www.googleapis.com/auth/userinfo.profile' // ... ] - }) + }); + +### with API keys + angular.module('myApp') + .value('GoogleApp', { + apiKey: 'YOUR_API_KEY' + }); To use a specific service, inject it into your controllers by name. All GAPI methods return a promise.