我正在尝试通过gobase应用脚本中的firebase rest API使用OAuth2登录用户。我正在使用apps-script-oauth2库,并使用service.getAccessToken()
(see getAccessToken docs)检索了一个访问令牌。
但是,当我使用以下代码发布到auth端点时:
var oAuthService= getOAuthService();
const createUserWithOAuthUrl = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyAssertion?key=' + <API_KEY>
const accessToken = oAuthService.getAccessToken()
Logger.log(accessToken)
const requestUri = 'https://script.google.com/macros/d/' + SCRIPT_ID + '/usercallback'
const payload = JSON.stringify({"postBody": "access_token=" + accessToken + "&providerId=google.com","requestUri":requestUri,"returnIdpCredential":true,"returnSecureToken":true})
Logger.log(payload)
const result = UrlFetchApp.fetch(createUserWithOAuthUrl, {
method: 'post',
contentType: 'application/json',
muteHttpExceptions: true,
// headers: {
// Authorization: 'Bearer ' + accessToken
// },
payload : payload
});
我收到此错误:
"error": {
"code": 400,
"message": "INVALID_IDP_RESPONSE : Failed to fetch resource from https://www.googleapis.com/oauth2/v1/userinfo, http status: 401, http response: {\n \"error\": {\n \"code\": 401,\n \"message\": \"Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.\",\n \"status\": \"UNAUTHENTICATED\"\n }\n}\n",
当我通过帐户选择/范围审核流程并获得授权提示时,OAuth流程似乎进展顺利。
我跟着firebase rest auth api docs,它看起来像我这样做的方式是正确的,所以我很困惑为什么它被拒绝。有什么想法吗?有什么方法可以测试我的access_token的有效性吗?我控制台记录了它,它看起来肯定像。
好的,所以我只想出来,它与范围有关。
基本上在firebase登录OAuth流程期间,会自动调用https://www.googleapis.com/oauth2/v1/userinfo
api。这需要以下范围:
https://www.googleapis.com/auth/userinfo.email
https://www.googleapis.com/auth/userinfo.profile
我想firebase需要将特定的用户详细信息添加到用户帐户。不知道为什么它抱怨代币而不是范围,肯定会把我扔掉!