当前位置: 首页 > 面试题库 >

Google Cloud端点(应用程式引擎)+与Android的oauth2整合

诸葛绍元
2023-03-14
问题内容

我正在尝试将google app引擎云终结点API与android集成。

这是我的Android应用程式辅助程式码和应用程​​式引擎端点程式码:

**<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”。

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


问题答案:

解决的问题:使用错误的SHA1创建android-client-id。该链接帮助我确定了我的错误

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



 类似资料:
  • 尝试将在app engine标准环境中部署的spring boot应用程序与云MySql连接时,会引发以下错误。 GCLOUD:java.sql.SQLNon瞬态连接异常:无法连接到MySQL服务器在localhost: 3,306。GCLOUD:确保有一个MySQL服务器运行在您试图连接到的机器/端口上,并且运行此软件的机器能够连接到此主机/端口(即没有防火墙)。还要确保服务器尚未使用--ski

  • 我是谷歌应用引擎服务的新手。我有一个JavaMaven项目,其中一个模块运行在应用引擎flex上,另一个模块运行在应用引擎标准上。我正在为应用引擎Flex API使用JWT身份验证。我想从应用引擎标准向应用引擎Flex发出发布请求。验证服务的最佳方式应该是什么? 此外,我还有一个cron服务,它可以访问我用于某些后端内容的特定URL。如何验证请求是否仅来自Cron服务?

  • 我有一个导入jar的app引擎app。在那个罐子里,我用的是GoogleClientSecrets。load()以加载client\u机密。用于使用BigQuery进行身份验证的json文件。显然,当我在localhost上部署应用程序时,应用程序引擎不喜欢我从磁盘上的某个位置读取文件。我假设,如果我把凭据放在WEBINF文件夹中,它会工作,但还没有测试,但这样任何人都可以轻松访问该文件。哪里是放

  • 我正在寻找关于如何建立一个谷歌应用引擎项目的建议,该项目涉及多个模块,其中一个模块是GWT项目。我已经阅读了谷歌应用引擎模块文档(https://developers.google.com/appengine/docs/java/modules/)并在Stackoverflow上找到了一些帮助,比如本文使用appengine骨架原型发布GWT GAE应用程序引擎模块。然而,这涉及到很多东西,所以我

  • 我有一个Gradle App Engine项目,我正在使用Google ApppEngine开发服务器进行调试。

  • 我正在尝试在Linux的Windows子系统上运行Google App Engine的最新Go SDK。我想将它绑定到端口8080,但系统一直试图绑定到端口80,但失败了。 $dev_appserver。py——端口8080。 INFO2018-06-08 02:48:56,648devappserver2.py:120]跳过SDK更新检查。 INFO2018-06-08 02:48:56,786