代表 google 家庭用户访问 google api

Accessing google apis on behalf of google home user

我正在尝试开发一个简单的 ProofOfConcept,以通过 Dialogflow Fullfilment 使用用户授权与来自 Google 家庭的 Google API 进行交互。

例如,我们希望能够create/read/update/delete 联系用户。

实现 Google 登录相对容易,但我不知道如何请求额外的范围

此外,我以前从未实施过 OAuth 服务器,我仍然不确定我是否也需要,或者我是否可以重用现有的服务器。

我在这里看了很多帖子所以大多数都是@Prisoner 回答的,他还在

中提供了详细的流程

在他的回答中,他提到我们可以 "redirect the user to a web login page" 但我仍然不明白如何从 fullfilment

这是我用来使用 Google 登录的完整代码:

//requirements
const { dialogflow, SignIn} = require('actions-on-google');
const functions = require('firebase-functions');

//initialisation
const app = dialogflow(
{
    debug: true,

    // REPLACE THE PLACEHOLDER WITH THE CLIENT_ID OF YOUR ACTIONS PROJECT
    clientId: '1111111111111-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.apps.googleusercontent.com'
});

//Welcome Intent
app.intent('Default Welcome Intent', (conv) => {
    conv.ask(`Welcome to the demo agent, say "Connect-me" to proceed`);
}); 

//Default Intent
app.intent('Default Fallback Intent', (conv) => {
    conv.ask(`Sorry, can you repeat please?`);
}); 

//Intent starting the Google SignIn
//  Create an intent with the name "Start Signin"
app.intent('Start Signin', (conv) => {
    //SignIn Helper (https://developers.google.com/actions/assistant/helpers#account_sign-in)
    conv.ask(new SignIn(`Need to identify you`));
});


//Intent called when the user complete the authentication
//  Create an intent with the name "Get Signin" and assign it the event "actions_intent_SIGN_IN"
app.intent('Get Signin', (conv, params, signin) => {
    if (signin.status === 'OK') {
        const payload = conv.user.profile.payload;

        conv.ask(`Hello ${payload.name}. You can now access your contacts informations...  but not really `);
    } else {
        conv.ask(`Sorry but you should authentify yourself `);
    }
});

//Example of intent that I would like to make it works
app.intent('How many contacts', (conv) => {
    /*
    TODO: How to ask for additional scopes ("https://www.google.com/m8/feeds/" : read/write access to Contacts and Contact Groups)
    NOTE: Actually, I'm more interrested on how to get the additional scopes.  
          I could probably do the querying contacts part by myself since it's quite documented (https://developers.google.com/contacts/v3/)
    */

    conv.ask(new SignIn(`You have ${nbContacts} contacts defined in your Google Contact`));
});

exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

您有几个相关问题捆绑在这里。

如何使用 Google 登录请求额外的范围?

很遗憾,你不能。

这是为了用户的安全。由于当您通过语音授予额外权限时并不总是很明显,并且要求太多的范围可能会被语音压倒,所以他们现在不允许这样做。

好的。那么如何获得这些额外的范围?

您引用了另一个 post,它采用了我建议的方法。它涉及通过同一 Google 云项目中的网页登录和授予范围。在 Google 的 cross-client identity 系统下,当用户通过智能助理连接时,这些也会适用。

那么如何将他们引导至网页?

我通常使用 card with a link button for the website or a link out suggestion to the site. Both of these, however, depend on being on a visual interface that supports a web browser. So you might wish to check for this using surface capabilities

如果您使用自己的 OAuth 服务器,使用组合 "OAuth and Google Sign-In" method,助手会为您处理这些事情。

我需要编写自己的 OAuth 服务器吗?

没有。您可以使用 Auth0 之类的东西来为您处理 OAuth 部分。