2015-04-03 44 views
0

我想要將我的服務終端的訪問權限限制爲僅部署在Google App Engine上的網絡應用,因此只能訪問some-app-id.appspot.com和映射到它的域mymappeddomain.com。我也不希望用戶登錄才能使API運行。所以,基本上我希望API只能被託管在相同Google App Engine應用程序實例中的JavaScript代碼訪問,而不需要用戶登錄以使用該服務。理想情況下,我的API也無法在API Explorer中查看。限制對Google App Engine終端的訪問

我已經研究了一下,發現了一些文章(sample codewalk through),但還沒有成功應用它們。此外,似乎所有的解決方案似乎仍然需要用戶登錄。我使用Google Developer Console創建了一個Web應用程序客戶端ID。

端點使用Go編寫並部署到Google App Engine。它類似於此:

package my 
import (... "github.com/GoogleCloudPlatform/go-endpoints/endpoints" ...) 

func (ms *MyService) ListData(c endpoints.Context, r *MyRequest) (*MyResponse, error) { 
_, err := endpoints.CurrentBearerTokenUser(c, 
    []string{ endpoints.EmailScope }, 
    []string{ "some-long-id-matching-what-is-used-in-javascript.apps.googleusercontent.com" }) 

if err != nil { 
    log.Printf("auth failed") 
    return nil, err 
} 
... 

我的JavaScript看起來像:

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

app.run(['$window', 'GAuth', 'GApi', function ($window, GAuth, GApi) { 
    var CLIENT = 'some-long-id-matching-what-is-used-in-go.apps.googleusercontent.com'; 
    var BASE; 

    if($window.location.hostname === 'localhost') { 
    BASE = '//localhost:8080/_ah/api'; 
    } else { 
    BASE = 'https://my-app-id.appspot.com/_ah/api'; 
    } 

    GApi.load('myservice', 'v1', BASE); 
    GAuth.setClient(CLIENT); 
    GAuth.setScopes('https://www.googleapis.com/auth/userinfo.email'); 

    GAuth.checkAuth().then(function() { 
    console.log('authenticated'); 
    }, function() { 
    console.log('authentication issue'); 
    }); 
}]); 

當我從瀏覽器中運行這個程序,我看到在JavaScript控制檯以下錯誤:

angular-google-gapi.min.js:7 myservice v1 api loaded 
https://content.googleapis.com/oauth2/v2/userinfo Failed to load resource: the server responded with a status of 401 (OK) 
app.js:24 authentication issue 

我很樂意就如何開展這項工作提出建議。先謝謝你。

回答

0

GAuth.checkAuth()它只是一種方法來測試用戶是否已經登錄您的應用程序並在用戶可能的情況下自動連接用戶。要第一次登錄,您必須使用方法GAuth.login()。看看這裏的例子:https://github.com/maximepvrt/angular-google-gapi/tree/gh-pages

+0

所以,如果我叫'GAuth.login()',它會導致最終用途來與他們的谷歌帳戶登錄?我不希望用戶登錄,我只想將我的API的訪問權限限制在我的域或本地主機上。我不希望有人看我的JS代碼,並能夠從其他應用程序調用我的API。 – SimplicityGuy 2015-04-07 23:08:22

+0

這是不可能的,在你的谷歌開發控制檯中,你已經添加了所有可以在你的api登錄中訪問的域名。如果你想減少某些用戶的登錄名,你可以檢查用戶的電子郵件是否存在於你的數據庫的用戶列表中。 – 2015-04-08 08:39:58

+0

'用戶'是來自網站的任何匿名用戶打我的網站,但是,我需要對API的訪問限制,因爲我的API確實存儲了用戶輸入的數據,並且我不想讓任意人使用API​​來獲取數據或者建立在我的API之上。我在我的開發控制檯中指定客戶端ID只能從mydomain.com和localhost訪問。那麼我是否仍然使用GAuth.login(),如果是,那麼我使用的是哪些用戶憑證?我不希望用戶輸入任何登錄憑據。 – SimplicityGuy 2015-04-08 13:36:14

0

我認爲你可能會爲你工作的只是在每個端點處理程序中設置一個'Access-Control-Allow-Origin'標頭來阻止跨域請求[1]。這是通過域鎖定某些資源給Javascript客戶端的正常習慣用法。

[1] http://enable-cors.org/server_appengine.html

相關問題