如何将请求发布到Firebase云功能扑http

时间:2019-04-22 07:00:12

标签: firebase flutter

我正在尝试从Flutter应用向我的Cloud Firebase函数发出http发布请求。

我确实从云功能日志中获取了以下错误消息

 var response = await http.post('$functionsUrl/userlocation/', body: {});
  print('Response status: ${response.statusCode}');

这是我的http请求代码。

export const setUserLocation = functions.https.onRequest( async (req, res) => {
  console.log(req);
});

我的问题是如何在flutter中设置标头,以便所有对云功能的客户端请求都带有带有verifyIdToken()的授权承载。

这是我的示例云函数:

function connectWithGooglePhotos()
{
$clientSecretJson = json_decode(
    file_get_contents('credentials.json'),
    true
)['web'];
$clientId = $clientSecretJson['client_id'];
$clientSecret = $clientSecretJson['client_secret'];
$tokenUri = $clientSecretJson['token_uri'];
$redirectUri = $clientSecretJson['redirect_uris'][0];
$scopes = ['https://www.googleapis.com/auth/photoslibrary'];

$oauth2 = new OAuth2([
    'clientId' => $clientId,
    'clientSecret' => $clientSecret,
    'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
    'redirectUri' => $redirectUri,
    'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
    'scope' => $scopes,
]);

// The authorization URI will, upon redirecting, return a parameter called code.
if (!isset($_GET['code'])) {
    $authenticationUrl = $oauth2->buildFullAuthorizationUri(['access_type' => 'offline']);
    header('Location: ' . $authenticationUrl);
} else {
    // With the code returned by the OAuth flow, we can retrieve the refresh token.
    $oauth2->setCode($_GET['code']);
    $authToken = $oauth2->fetchAuthToken();
    $refreshToken = $authToken['access_token'];

    // The UserRefreshCredentials will use the refresh token to 'refresh' the credentials when
    // they expire.
    $_SESSION['credentials'] = new UserRefreshCredentials(
        $scopes,
        [
            'client_id' => $clientId,
            'client_secret' => $clientSecret,
            'refreshToken' => $refreshToken,
        ]
    );

    $photosLibraryClient = new PhotosLibraryClient(['credentials' => $_SESSION['credentials']]);
}

return $photosLibraryClient;
}

0 个答案:

没有答案