获取Google实例ID Api进行授权时遇到问题

时间:2019-03-19 19:50:48

标签: python firebase oauth-2.0 google-api google-oauth2

我正在尝试致电POST => https://iid.googleapis.com/v1/web/iid端点服务器端。大多数Firebase API使用服务器访问密钥来授权请求​​,但是此终结点使用OAuth2。我很难让它与OAuth2一起使用。

Crypto-Key来自哪里?文档将其标记为APPLICATION_PUBLIC_KEY-哪个应用程序?我在哪里找到这个?在FCM控制台中没有任何东西像给定的示例。

这是我的尝试-

from requests import request
from google.auth.transport import requests as google_requests
from google.oauth2 import service_account

_request = google_requests.Request()

# Get access token

creds = service_account.Credentials\
    .from_service_account_info(fcm_credentials_json, scopes=['https://www.googleapis.com/auth/firebase.messaging',
                                                             'https://www.googleapis.com/auth/firebase'])
creds.refresh(_request)

access_token = creds.token

# Build headers/body

headers = {
  'Authorization': f'Bearer {access_token}',
  'Crypto-Key': f'p256ecdsa={app_public_key}',
  'Content-Type': 'application/json'
}

body = {
    "endpoint": "https://fcm.googleapis.com/fcm/send/fa9poLC00i0:APA91bHA...lnQ_SuwH-B_odM",
    "keys": {
        "p256dh": "BHz0KeciXJ...oE2yBqBc",
        "auth": "Cmchq...SG6g"
    }
}

# Make HTTP Request

res = request(
    method='POST',
    url='https://iid.googleapis.com/v1/web/iid',
    json=body,
    headers=headers
)

print(res.reason)
print(res.content)

结果:

  

未经授权

     

{    “错误”:{      “代码”:401,      “ message”:“请求缺少必需的身份验证凭据。期望的OAuth 2访问令牌,登录cookie或其他有效的身份验证凭据。请参见https://developers.google.com/identity/sign-in/web/devconsole-project。”,      “状态”:“未经授权”    }   }

0 个答案:

没有答案