使用 Android GoogleSignIn 和 GmailScopes.GMAIL_SEND (gmail api)

Using Android GoogleSignIn with GmailScopes.GMAIL_SEND (gmail api)

我想使用 GoogleSignIn 并使用 android 内部电子邮件地址 (gmail) 发送电子邮件

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
  //.requestIdToken(getString(R.string.default_web_client_id))
  .requestEmail()
  .requestScopes(new Scope(GmailScopes.GMAIL_SEND) )
  .build();

// Build a GoogleSignInClient with the options specified by gso.
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
dependencies {
  implementation 'com.google.api-client:google-api-client:1.23.0'
  implementation 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
  implementation 'com.google.apis:google-api-services-gmail:v1-rev83-1.23.0'
  // ...
}

我在 google 控制台 API 密钥中设置,用于使用 gmail 和 oauth 添加我的 sh1。

  1. 我不知道把 API 键放在应用程序的什么地方
  2. 使用 google 登录在没有
  3. 的情况下工作正常

.requestScopes(新范围(GmailScopes.GMAIL_SEND))

但是当我添加它时,它永远挂起。

在 GmailQuickstart 快速入门指南中,您拥有:

    private static final String TOKENS_DIRECTORY_PATH = "tokens";
    private static final String CREDENTIALS_FILE_PATH = "/credentials.json";

.
.
.

private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT,Context context, String personId) throws IOException {
    // Load client secrets.
    InputStream in = GmailQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
    if (in == null) {
        throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
    }

您需要下载 credentials.json 并将其放在此处:

在此处查看有关如何在 android 中集成 Gmail api 的完整答案: