谷歌云端点(app engine)+ oauth2与android集成

我正在尝试将谷歌应用引擎云端点API与Android集成。

我已经按照这个链接进行了相同的操作:

  1. 无法使用我的服务对象连接到我的Google端点

  2. https://cloud.google.com/appengine/docs/java/endpoints/consume_android

  3. 基于Java的Google App Engine,Android和身份validationoauth2

这是我的Android应用程序端代码和应用程​​序引擎端点代码:

**** @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Profile profile = new Profile(); profile.setDisplayName("Alex roger"); profile.setAge(49l); new EndpointsAsyncTask(this).execute(profile); } **** public class EndpointsAsyncTask extends AsyncTask { private MyApi myApiService; private Context context; SharedPreferences settings; GoogleAccountCredential credential; private static final String PREF_ACCOUNT_NAME = "PREF_ACCOUNT_NAME"; public EndpointsAsyncTask(Context context) { this.context = context; } @Override protected Profile doInBackground(Profile... params) { Profile profile = params[0]; try { settings = context.getSharedPreferences(PREF_ACCOUNT_NAME, Context.MODE_PRIVATE); credential = GoogleAccountCredential.usingAudience(context, "server:client_id:.apps.googleusercontent.com"); credential.setSelectedAccountName("@gmail.com"); MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), credential); builder.setApplicationName(context.getPackageName()); builder.setRootUrl("https://.appspot.com/_ah/api/"); myApiService = builder.build(); return myApiService.saveProfile(profile.getAge(), profile.getDisplayName()).execute(); } catch (IOException e) { Log.e("IOException", String.valueOf(e.getCause() +" " +e)); } catch (Exception e) { Log.e("Exception", e+""); } return profile; } @Override protected void onPostExecute(Profile result) { Log.e("Display Name", result.getDisplayName()); } } **** @Api( name = "myApi", version = "v1", namespace = @ApiNamespace( ownerDomain = "backend.endpoint", ownerName = "backend.endpoint", packagePath = "" ), scopes = {Constant.API_EMAIL_SCOPE}, clientIds = {".apps.googleusercontent.com", ".apps.googleusercontent.com", Constant.API_EXPLORER_CLIENT_ID}, audiences = {".apps.googleusercontent.com"} ) public class MyEndpoint { @ApiMethod(name = "saveProfile",path = "saveProfile", httpMethod = ApiMethod.HttpMethod.POST) public Profile saveProfile(@Named("name") String name, @Named("age") long age, User user) { Profile profile = new Profile(name,age); ofy().save().entity(profile).now(); return profile; } @ApiMethod(name = "saveProfile1",path = "saveProfile1", httpMethod = ApiMethod.HttpMethod.POST) public Profile saveProfile1(@Named("name") String name, @Named("age") long age) { Profile profile = new Profile(name,age); ofy().save().entity(profile).now(); return profile; } } **** @Entity @Cache public class Profile { String displayName; @Id long age; private Profile(){} public Profile(final String displayName,final long age){ this.displayName = displayName; this.age = age; } public String getDisplayName() { return displayName; } public long getAge() { return age; } } **** public class OfyService { static { factory().register(Profile.class); } public static Objectify ofy() { return ObjectifyService.ofy(); } public static ObjectifyFactory factory() { return ObjectifyService.factory(); } } 

但是收到错误“IOException:com.google.android.gms.auth.GoogleAuthException:Unknown com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException”。

如果我们不使用oAuth2服务(用户用户),那么它的工作正常。 还建议一些有用的链接。

问题已解决:使用错误的SHA1创建android-client-id。 这个链接帮助我识别我的错误:

“ Google端点 – Android GoogleAuthIOException Tic Tac Toe – 删除了clientIds ”

注意:debug.keystore文件可以在c:\ Users \ .android \中找到