相关疑难解决方法(0)

Gmail REST API:400错误请求+失败的前提条件

我正在尝试使用google java api服务发送基于Gmail REST API的邮件.我已通过Google Develover Console配置了应用程序客户端并下载了p12和json文件.

我使用过这个示例程序,https://developers.google.com/gmail/api/guides/sending#sending_messages ...

此示例有效,但这是基于GoogleAuthorizationCodeFlow.我只想从服务器到服务器工作,直接调用,而不是打开浏览器来获取访问令牌......我得到了它(访问令牌)但最后我收到了错误请求....为什么? ?我收到的信息不仅仅是"Bad Request"和"Precondition Failed"

基于此,我按照以下步骤操作:

  1. 第一步:根据我的客户帐户邮件和p12生成的文件创建GoogleCredential对象:

    GoogleCredential  credential = new GoogleCredential.Builder().setTransport(new NetHttpTransport())
                                        .setJsonFactory(new JacksonFactory())
                                        .setServiceAccountId(serviceAccountUserEmail)
                                        .setServiceAccountScopes(scopes)
                                        .setServiceAccountPrivateKeyFromP12File(
                                                    new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))                             
                                                            .build();
    
    Run Code Online (Sandbox Code Playgroud)

在这里,我必须指出,我有很多问题需要使用ClientID而不是ClientMail.它必须使用@ developer.gserviceaccount.com帐户而不是.apps.googleusercontent.com.如果你不发送这个参数确定,你得到一个"无效授予"错误.这在此处说明:https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtAuthorization

  1. 第二步:根据凭据创建Gmail服务:

    Gmail gmailService = new Gmail.Builder(httpTransport,
                                                  jsonFactory,
                                                  credential)
                                                .setApplicationName(APP_NAME)
                                                    .build();
    
    Run Code Online (Sandbox Code Playgroud)
  2. 第三步从MimmeMessage创建Google原始消息:

    private static Message _createMessageWithEmail(final MimeMessage email) throws MessagingException, IOException {
    
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    email.writeTo(bytes);
    String encodedEmail =       Base64.encodeBase64URLSafeString(bytes.toByteArray());      
    Message message = new Message();    
    message.setRaw(encodedEmail);
    return message;
    
    Run Code Online (Sandbox Code Playgroud)

    }

  3. 第四步调 …

java rest google-cloud-endpoints gmail-api

12
推荐指数
1
解决办法
1万
查看次数

GAE AttributeError: 'Credentials' 对象没有属性 'with_subject'

我有一个 Python 应用程序,我想部署在 App Engine(第二代 Python 3.7)上,我在该应用程序上使用启用了域范围委派的服务帐户来访问用户数据。

在本地我这样做:

import google.auth
from apiclient.discovery import build

creds, project = google.auth.default(
    scopes=['https://www.googleapis.com/auth/admin.directory.user', ],
)
creds = creds.with_subject(GSUITE_ADMIN_USER)

service = build('admin', 'directory_v1', credentials=creds)
Run Code Online (Sandbox Code Playgroud)

这很好用,据我所知,这是当前使用应用程序默认凭据时执行此操作的方法(在本地我定义了 GOOGLE_APPLICATION_CREDENTIALS)。

问题出在 GAE 上,部署后,调用会with_subject引发: AttributeError: 'Credentials' object has no attribute 'with_subject'

我已经在 GAE 服务帐户上启用了域范围的委派。

当我在本地使用的 GOOGLE_APPLICATION_CREDENTIALS 和 GAE 中的那些都是具有域范围委派的服务帐户时,它们之间有什么区别?

.with_subject()GAE在哪里?

creds收到的对象是 类型compute_engine.credentials.Credentials

完整追溯:

Traceback (most recent call last):
  File "/env/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/env/lib/python3.7/site-packages/gunicorn/workers/gthread.py", line 104, in init_process
    super(ThreadWorker, …
Run Code Online (Sandbox Code Playgroud)

python google-app-engine google-authentication google-api-python-client

5
推荐指数
1
解决办法
3795
查看次数