如何执行服务器到服务器的Google请求

huangapple go评论52阅读模式
英文:

How to perform server to server Google requests

问题

I can provide a translation of the non-code parts of your text:

"我正在开发一个SAAS应用程序,前端使用Vue,后端使用Symfony作为API REST。我已经在Google客户端控制台中配置了所需的所有内容,前端通过JavaScript库执行用户OAuth2身份验证,授予应用程序访问权限,获取令牌和刷新令牌。在这一点上,前端会向后端发出POST请求,将令牌存储在数据库表中。现在我想要做的是(我无法从Google获取信息的部分):

  • 前端请求从后端获取或存储资源(我们只在Drive或Calendar中工作)
  • 我的后端使用OAuth信息执行与Google相关的请求(如何?)
  • 我的后端处理Google的响应,如果需要,存储一些数据,并返回响应,最终包括资源信息/链接,发送给前端。

我能找到的所有信息都与如何执行OAuth请求有关,但没有关于如何使用令牌的内容。

感谢所有的帮助。"

英文:

I'm developing a SAAS application with a FE in Vue and an API REST in Symfony as BE.
I have configured all I need in the Google Client Console and the FE throught a JavaScript library performs a user oAuth2 authentication, grant the app, ecc untill get the token end the refresh token.
At this point the FE makes a POST request to my BE to store the tokens in a DB table.
What I want to do now (and what that I was not be able to get infos from Google) is:

  • FE asks to get or store resources to my BE (we works only in Drive or Calendar)
  • my BE uses the oAuth information to perform the related request to Google (How???)
  • my BE works the Google response, stores some data if needed and returns a response, eventually with resources infos/links, to my FE

Every informations that I was able to find relates to how to perform an oAuth request but nothing about how to use the tokens.

Thanks to all

答案1

得分: 1

Solution was simple: just give the token and eventually refresh token to the Google\Client object:

private function getClient(ExternalResource $resource) {
  $client = new \Google\Client();
  $client->setAccessToken($resource->getToken());
  $client->setClientId($_ENV["GOOGLE_CLIENT_ID"]);
  $client->setClientSecret($_ENV["GOOGLE_CLIENT_SECRET"]);

  if ($client->isAccessTokenExpired()) {
    $token = $client->fetchAccessTokenWithRefreshToken($resource->getRefreshToken());

    if (!$token || !isset($token["access_token"])) {
      return null;
    }

    $this->externalResourceRepository->updateResource(
      $resource,
      ["token" => $token["access_token"]]
    );
    $client->setAccessToken($resource->getToken());
  }

  return $client;
}

Thnks ADyson for help.

英文:

Solution was simple: just give the token and eventually refresh token to the Google\Client object:

private function getClient(ExternalResource $resource) {
  $client = new \Google\Client();
  $client->setAccessToken($resource->getToken());
  $client->setClientId($_ENV["GOOGLE_CLIENT_ID"]);
  $client->setClientSecret($_ENV["GOOGLE_CLIENT_SECRET"]);

  if ($client->isAccessTokenExpired()) {
    $token = $client->fetchAccessTokenWithRefreshToken($resource->getRefreshToken());

    if (!$token || !isset($token["access_token"])) {
      return null;
    }

    $this->externalResourceRepository->updateResource(
      $resource,
      ["token" => $token["access_token"]]
    );
    $client->setAccessToken($resource->getToken());
  }

  return $client;
}

Thnks ADyson for help.

huangapple
  • 本文由 发表于 2023年4月17日 17:04:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定