[英]Google cloud endpoints (app engine) + oauth2 integration with android
我正在嘗試將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。 該鏈接幫助我確定了我的錯誤:
“ Google端點-Android GoogleAuthIOException井字游戲-刪除了clientIds ”
注意:debug.keystore文件可以在c:\\ Users \\ .android \\中找到
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.