提问人:Mohamed Amine 提问时间:11/17/2023 更新时间:11/17/2023 访问量:11
如何在 Android Java 11 上使用客户端 ID 获取 OAuth 2.0 访问令牌,我只有客户端 ID,因为我选择应用程序类型 android
how to get OAuth 2.0 access token using client id on Android java 11 , i have just client id because i choose application type android
问:
创建 OAuth 2.0 凭据:
转到 Google Cloud Console。 创建新项目或选择现有项目。 导航到“API 和服务”>“凭据”。 创建 OAuth 2.0 客户端 ID。 记下生成的客户端 ID;这将在您的 Android 应用程序中使用。 像这样
在 Android 上使用客户端 ID OAuth2.0 获取访问令牌
`
>package com.applist.myapplication;
>import androidx.appcompat.app.AppCompatActivity;
>import android.os.Bundle;
>import android.content.Intent;
>import com.google.android.gms.auth.api.signin.GoogleSignIn;
>import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
>import com.google.android.gms.auth.api.signin.GoogleSignInClient;
>import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
>import com.google.android.gms.common.api.ApiException;
>public class MainActivity extends AppCompatActivity {
>private static final int RC_SIGN_IN = 9001;
>private GoogleSignInClient mGoogleSignInClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken("1004241379278-ejf1eq1in0dhd2divhp9akd6l47ndlrj.apps.googleusercontent.com")
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
signIn();
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
try {
GoogleSignInAccount account = GoogleSignIn.getSignedInAccountFromIntent(data).getResult(ApiException.class);
String accessToken = account.getIdToken();
System.out.println((accessToken));
} catch (ApiException e) {
// Handle error
}
}
}`
> i found this code but not working : startActivityForResult ='startActivityForResult(android.content.Intent, int)' is deprecated `/ ```
答: 暂无答案
评论