1

我一直在使用谷歌應用程序引擎的樣品(移動購物助理)https://github.com/GoogleCloudPlatform/MobileShoppingAssistant-sample/tree/master/MobileAssistantAndroidAppEngine。我花了大約一週的時間來設置項目並修復問題(主要與依賴和配置有關)。現在後端部署了,我可以通過api explorer訪問api。我可以執行非受限方法並從服務器獲取響應,但是當我執行管理方法時,即使在打開OAuth時也會收到錯誤。註冊與後端應用程序失敗發動機的示例代碼

OK 401 
message: Only authenticated users may invoke this operation 

,所以我認爲這個問題是由於不良的配置

這裏是我做了什麼:

應用gradle.build

apply from: 'config.gradle' 
apply plugin: 'com.android.application' 


android { 
compileSdkVersion 21 
buildToolsVersion "21.1.2" 

defaultConfig { 
    applicationId "com.google.sample.mobileassistant" 
    minSdkVersion 17 
    targetSdkVersion 21 
    versionCode 1 
    versionName "1.0" 
} 
buildTypes { 
    debug { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
compileOptions { 
    sourceCompatibility JavaVersion.VERSION_1_7 
    targetCompatibility JavaVersion.VERSION_1_7 
} 


} 

dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) 
compile 'com.android.support:appcompat-v7:21.0.3' 
compile 'com.google.android.gms:play-services:6.5.87' 
compile project(path: ':backend', configuration: 'android-endpoints') 
} 

應用構建配置

buildConfigField "String", "ROOT_URL", 
      "\"https://MY_PROJECT_ID.appspot.com/_ah/api/\"" 

    buildConfigField "boolean", "SIGN_IN_REQUIRED", "true" 

    buildConfigField "String", "SENDER_ID", "\"MY_SENDER_ID\"" 

    buildConfigField "String", "WEB_CLIENT_ID", "\"MY_WEB_CLIENT_ID\"" 

而且build.gradel的後端中,我已經改變了應用程序引擎的SDK版本,從18至30

>>注:機器人工作室的檢查報告,下,appcfg端點符號無法解析!

buildscript { 
repositories { 
    mavenLocal() 
    mavenCentral() 
} 
dependencies { 
    classpath 'com.google.appengine:gradle-appengine-plugin:1.9.30' 
    } 
} 

repositories { 
mavenCentral(); 
} 

apply plugin: 'java' 
apply plugin: 'war' 
apply plugin: 'appengine' 

sourceCompatibility = JavaVersion.VERSION_1_7 
targetCompatibility = JavaVersion.VERSION_1_7 


dependencies { 
appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.30' 
compile 'com.google.appengine:appengine-endpoints:1.9.30' 
compile 'com.google.appengine:appengine-endpoints-deps:1.9.30' 
compile 'com.google.appengine:appengine-api-1.0-sdk:1.9.30' 
compile 'javax.servlet:servlet-api:2.5' 
compile 'com.googlecode.objectify:objectify:5.1.5' 
compile 'com.ganyo:gcm-server:1.0.2' 
} 

appengine { 
downloadSdk = true 
appcfg { 
    oauth2 = true 
} 
endpoints { 
    getClientLibsOnBuild = true 
    getDiscoveryDocsOnBuild = true 
} 


} 

項目ID在appengine_web.xml以及API密鑰設置。當我運行的客戶端應用程序獲得REG_ID但註冊失敗由於GoogleAuthIOException:

E/MainActivity: Exception=com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException 

其從sendRegistrationIdToBackend()方法拋出:

/** 
* Sends the registration ID to your server over HTTP, so it can use 
* GCM/HTTP or CCS to send messages to your app. Not needed for this 
* demo since the device sends upstream messages to a server that echoes 
* back the message using the 'from' address in the message. 
*/ 
private void sendRegistrationIdToBackend() { 
    try { 
     shoppingAssistantAPI.registrations() 
       .registerDevice(regId).execute(); 
     // Persist the registration ID - no need to register again. 
     storeRegistrationId(context, regId); 
    } catch (IOException e) { 
     LOG.warning("Exception when sending registration ID to the " 
       + "backend = "+ e.getMessage()); 
     // If there is an error, we will try again to register the 
     // device with GCM the next time the MainActivity starts. 
    } 
} 

現在這個方法調用

shoppingAssistantAPI.registrations() 
       .registerDevice(regId).execute(); 

但是在後端的源代碼有不同的類名和方法簽名...類Registration.class與getter和setter方法& RegistrationEndpoint枝條registerDevice(字符串的RegID,UserAccount用戶)

而這正是我感到困惑? 有人可以向我解釋這個代碼有什麼問題,以及我如何讓客戶端應用程序正確地與後端通信。

--- UPDATE ---

客戶端應用程序請求的方法(chickIn法),這需要一個說法,尚不可用(REGID)這種奇怪的!

03-01 20:07:48.898 25083-25359/com.google.sample.mobileassistant W/MainActivity: Exception when checking in =com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException 
03-01 20:07:51.581 25083-25359/com.google.sample.mobileassistant W/MainActivity: Exception when sending registration ID to the backend = null 
03-01 20:07:51.601 25083-25083/com.google.sample.mobileassistant I/MainActivity: Device registered, registration ID= APA91bF2eC6o9_KBI2coPMqqXE94lECzeYLnrD30nmK2PvmoHg3pFfVuxYkLir3HPV07gq-n-UZZOsM1EUSQOfR8WnWYaG1iDFbx0IuIOLNKIMdeDimPAH5H7DURDqZZ8hWdrsStnS4sEHHTB155bx91L7FpV0txHA 
03-01 20:07:52.292 25083-25359/com.google.sample.mobileassistant E/MainActivity: Exception=com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException 
+0

我記得你必須在開發者控制檯中設置你正在使用的用戶登錄管理區域的所有者。 – dmaij

+0

hi @dmaij ...我應該在哪裏添加用戶?...在​​配置文件中?可以解釋更多 – msdev16

+0

不,在Google開發者控制檯中,應該有一個項目所有者應該用於登錄到管理員部分。您應該無法使用設備實際登錄到管理員部分。 – dmaij

回答

0

這個工作從Android應用程序訪問非受限API方法......這可能是別人,因爲的OAuth的人正在遭受樂於助人......

我加服務器:CLIENT_ID:到服務器客戶端ID和它的工作:)

http://android-developers.blogspot.com/2013/01/verifying-back-end-calls-from-android.html

在Android應用

您需要致電Google Play服務 GoogleAuthUtil類才能獲取ID令牌;該過程如獲取訪問令牌所述爲 。還有一點額外的魔力:getToken(email,scope)方法的scope參數的 值。它的 必須是字符串受衆:server:client_id:X,其中X是 Web應用程序的客戶端ID,如上所述。如果我們的客戶端ID 是上面給出的示例值,則範圍參數 的值將爲 audience:server:client_id:9414861317621.apps.googleusercontent.com。