0

我在 Google App Engine 中使用 python 创建了一个项目。我想在我的网站上使用谷歌登录。

我尝试使用 Google 登录网络,它运行良好,但我不知道如何从服务器进行 API 调用以确保用户已登录。

我尝试使用 users ==> user = users.get_current_user() 然后使用用户 id 进行 API 调用 ==> " https://www.googleapis.com/plus/v1/people/ "+ self.request。 get('id') +"?fields=image&key=SOME_KEY"

问题是 id 不是我应该使用的。当我比较这个id和我在javascrip中得到的id时,它们是不一样的。此外,在 Web API 中,他们提到我不应该直接将该 id 发送到服务器(我不知道原因)。所以我想知道如何在我的 python 应用程序中获得正确的 id。

// 不要直接发送到你的服务器!

<html lang="en">
  <head>
    <meta name="google-signin-scope" content="profile email">
    <meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com">
    <script src="https://apis.google.com/js/platform.js" async defer></script>
  </head>
  <body>
    <div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
    <script>
      function onSignIn(googleUser) {
        // Useful data for your client-side scripts:
        var profile = googleUser.getBasicProfile();
        console.log("ID: " + profile.getId()); // Don't send this directly to your server!
        console.log('Full Name: ' + profile.getName());
        console.log('Given Name: ' + profile.getGivenName());
        console.log('Family Name: ' + profile.getFamilyName());
        console.log("Image URL: " + profile.getImageUrl());
        console.log("Email: " + profile.getEmail());

        // The ID token you need to pass to your backend:
        var id_token = googleUser.getAuthResponse().id_token;
        console.log("ID Token: " + id_token);
      };
    </script>
  </body>
</html>
4

2 回答 2

2

您不能使用用户 ID 进行 API 调用。当您根据 API 对谷歌服务器进行 API 调用时,您必须传递不同的参数。例如,如果您想列出您的 gmail 中可用的标签,您将去要使用 Gmail API,并且必须在获取请求中传递 labelid、userid。这是上述示例的文档。https://developers.google.com/gmail/api/v1/reference/users/labels/get 您可以使用 Google 提供的 Try this API box 来玩转这个 API。像这个例子一样,所有的谷歌产品都有不同的 API 来实现不同的功能,你必须根据他们提供的文档传递不同的参数。

于 2017-01-09T10:30:26.133 回答
0

在谷歌 API 中深入搜索后,我看到他们引入了一种发送 id 令牌的方法。更多信息可以在这里找到:

https://developers.google.com/identity/sign-in/web/backend-auth

于 2017-01-09T03:24:58.557 回答