![](/img/trans.png)
[英]how to use basic authentication in Android/IOS app with Google Cloud EndPoints(java)
[英]How to add authentication to google cloud endpoints?
從Google Cloud Endpoint井字游戲示例開始(請參閱https://github.com/GoogleCloudPlatform/appengine-endpoints-tictactoe-java )
然后,在最新的Android Ellipse插件中通過Ellipse創建一個“ AppEngine Connected Android項目”(請參閱https://developers.google.com/eclipse/docs/endpoints-androidconnected-gae )
現在,這將創建沒有身份驗證的端點。
// @Api(name = "messageEndpoint")
//NO AUTHENTICATION; OPEN ENDPOINT!
現在,以井字游戲應用程序為例,嘗試添加身份驗證。
@Api(name = "messageEndpoint",
version = "v1",
clientIds = {Ids.WEB_CLIENT_ID, Ids.ANDROID_CLIENT_ID, Ids.IOS_CLIENT_ID, com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID},
audiences = {Ids.ANDROID_AUDIENCE}
)
如果發生oauth異常,請添加以下內容:
//public void sendMessage(@Named("message") String message)
//throws IOException {
public void sendMessage(@Named("message") String message)
throws OAuthRequestException,IOException {
並使用api資源管理器https:// [your-app-id] .appspot.com / _ah / api / explorer進行測試,我用特定的應用程序ID替換了[your-app-id]。
POST https://[your-app-id].appspot.com/_ah/api/messageEndpoint/v1/sendMessage/test
X-JavaScript-User-Agent: Google APIs Explorer
503 Service Unavailable
cache-control: private, max-age=0
content-encoding: gzip
content-length: 193
content-type: application/json; charset=UTF-8
date: Mon, 01 Apr 2013 22:57:17 GMT
expires: Mon, 01 Apr 2013 22:57:17 GMT
server: GSE
{
"error": {
"errors": [
{
"domain": "global",
"reason": "backendError",
"message": "java.lang.NoClassDefFoundError: Could not initialize class com.brooklynmarathon.citysync.endpoints.EMF"
}
],
"code": 503,
"message": "java.lang.NoClassDefFoundError: Could not initialize class com.brooklynmarathon.citysync.endpoints.EMF"
}
}
我使用我下載的tic tac toe應用程序嘗試了瀏覽器(感謝Google Dev Rel),並在此處構建並上傳了該應用程序: https : //brooklynmarathon.appspot.com/_ah/api/explorer
當我嘗試訪問分數列表時,我看到:
GET https://brooklynmarathon.appspot.com/_ah/api/tictactoe/v1/score
X-JavaScript-User-Agent: Google APIs Explorer
Response
401 Unauthorized
- Hide headers -
cache-control: private, max-age=0
content-encoding: gzip
content-length: 193
content-type: application/json; charset=UTF-8
date: Tue, 02 Apr 2013 01:14:40 GMT
expires: Tue, 02 Apr 2013 01:14:40 GMT
server: GSE
www-authenticate: GoogleLogin realm="https://www.google.com/accounts/ClientLogin", service="xapi"
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "com.google.appengine.api.oauth.OAuthRequestException: Invalid user.",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "com.google.appengine.api.oauth.OAuthRequestException: Invalid user."
}
}
我的問題是為什么井字游戲示例的標頭為:“ www-authenticate:GoogleLogin realm =“ https://www.google.com/accounts/ClientLogin”,service =“ xapi”和
通常,我們如何向“與App Engine連接的Android項目”生成的端點添加身份驗證?
通過Web應用程序,Android客戶端或iOS客戶端,我們可以在api資源管理器中對其進行測試嗎?
“總的來說,我們如何向“與App Engine連接的Android項目”生成的端點添加身份驗證?”
您還將在那里(隨后的頁面)上看到有關iOS和Javascript的說明。
在服務器端,您需要將User對象添加為端點方法的第一個參數。
我不知道您的其他問題的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.