谷歌的云终端(应用程序引擎)+采用Android oauth2整合终端、应用程序、引擎、Android

2023-09-04 02:58:58 作者:男生大全

我试图整合谷歌应用程序引擎的云终端与Android API。

I am trying to integrate google app engine cloud endpoints API with android.

我按照这个链接是一样的:

I have followed this links for the same:

unable用我的服务对象连接到我的谷歌端点

https://cloud.google.com/appengine/docs /爪哇/终端/ consume_android

Java根据谷歌的App Engine,Android和认证oauth2

下面是我的Andr​​oid应用程序端code和应用程序引擎的端点code:

Here is my android app side code and app engine endpoints code:

**<Main activity>**

@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);
} 

**<EndpointAsyncTask>**

public class EndpointsAsyncTask extends AsyncTask<Profile, Void, Profile> {
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:<web-client-id>.apps.googleusercontent.com");
            credential.setSelectedAccountName("<myemail_logged_in>@gmail.com");

            MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory(), credential);
            builder.setApplicationName(context.getPackageName());
            builder.setRootUrl("https://<my-app-id>.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());
}

}

**<MyEndpoint>**

 @Api(
    name = "myApi",
    version = "v1",
    namespace = @ApiNamespace(
            ownerDomain = "backend.endpoint",
            ownerName = "backend.endpoint",
            packagePath = ""
    ),
    scopes = {Constant.API_EMAIL_SCOPE},
    clientIds = {"<web-client-id>.apps.googleusercontent.com",
            "<android-client-id>.apps.googleusercontent.com",
            Constant.API_EXPLORER_CLIENT_ID},
    audiences = {"<web-client-id>.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;
}
}

**<Profile.class>**

@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;
}

}

**<OfyService>**

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:未知com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException。

But getting error "IOException: com.google.android.gms.auth.GoogleAuthException: Unknown com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException".

如果我们不使用oAuth2服务(用户用户),那么它的工作是正确的。同时提出了一些有用的链接。

If we don't use oAuth2 service (User user) then it is working correct. Also suggest some useful links.

推荐答案

问题解决:使用错误的SHA1打造的Andr​​oid客户端ID。 这个链接帮我找出我的错误:

Problem resolved: Was using wrong SHA1 to create android-client-id. This link helped me to identify my error:

Google端点 - 安卓GoogleAuthIOException井字 - 删除的ClientID 的

注:debug.keystore文件可以在C中找到:\用户\ .android \

Note: debug.keystore file can be found in c:\Users\ .android\