简体   繁体   中英

Android: How to add authorization header on Google Chat API request

I'm new to using Google API and stuff. I'm trying to call Google Chat API from my android app. I noticed from the API docs that the endpoints require me to do some authentication first. 在此处输入图像描述

Then I put the Google Sign-In mechanism for my app, thinking it would somehow work. I tried to hit the endpoint with the requested body (Space) object, but it returns me the 401 code. Unauthenticated.

I/okhttp.OkHttpClient: --> POST https://chat.googleapis.com/v1/spaces
I/okhttp.OkHttpClient: Content-Type: application/x-www-form-urlencoded
I/okhttp.OkHttpClient: Content-Length: 148
I/okhttp.OkHttpClient: name=Space%20name&spaceType=DIRECT_MESSAGE&threaded=false&singleUserBotDm=true&spaceDetails=com.example.socketp2pmessagingapp.SpaceDetails%40ded35b2
I/okhttp.OkHttpClient: --> END POST (148-byte body)
I/okhttp.OkHttpClient: <-- 401 https://chat.googleapis.com/v1/spaces (516ms)
I/okhttp.OkHttpClient: www-authenticate: Bearer realm="https://accounts.google.com/"
I/okhttp.OkHttpClient: vary: Origin
I/okhttp.OkHttpClient: vary: X-Origin
I/okhttp.OkHttpClient: vary: Referer
I/okhttp.OkHttpClient: content-type: application/json; charset=UTF-8
I/okhttp.OkHttpClient: date: Tue, 23 Aug 2022 07:20:20 GMT
I/okhttp.OkHttpClient: server: ESF
I/okhttp.OkHttpClient: cache-control: private
I/okhttp.OkHttpClient: x-xss-protection: 0
I/okhttp.OkHttpClient: x-frame-options: SAMEORIGIN
I/okhttp.OkHttpClient: x-content-type-options: nosniff
I/okhttp.OkHttpClient: alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
I/okhttp.OkHttpClient: {
I/okhttp.OkHttpClient:   "error": {
I/okhttp.OkHttpClient:     "code": 401,
I/okhttp.OkHttpClient:     "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.",
I/okhttp.OkHttpClient:     "status": "UNAUTHENTICATED",
I/okhttp.OkHttpClient:     "details": [
I/okhttp.OkHttpClient:       {
I/okhttp.OkHttpClient:         "@type": "type.googleapis.com/google.rpc.ErrorInfo",
I/okhttp.OkHttpClient:         "reason": "CREDENTIALS_MISSING",
I/okhttp.OkHttpClient:         "domain": "googleapis.com",
I/okhttp.OkHttpClient:         "metadata": {
I/okhttp.OkHttpClient:           "method": "google.chat.v1.ChatService.CreateSpace",
I/okhttp.OkHttpClient:           "service": "chat.googleapis.com"
I/okhttp.OkHttpClient:         }
I/okhttp.OkHttpClient:       }
I/okhttp.OkHttpClient:     ]
I/okhttp.OkHttpClient:   }
I/okhttp.OkHttpClient: }
I/okhttp.OkHttpClient: <-- END HTTP (619-byte body)

I've no idea how I should fix this, where to put the access token for this request? Anyway, I use retrofit to make this call and the service code look like this.

    @FormUrlEncoded
    @POST("spaces")
    Call<Space> createSpace(
            @Field("name") String name,
            @Field("spaceType") SpaceType SPACE_TYPE_UNSPECIFIED,
            @Field("threaded") Boolean threaded,
            @Field("singleUserBotDm") Boolean singleUserBotDm,
            @Field("spaceDetails") SpaceDetails spaceDetails
    );

An example on how to implement it would be a great help. Thanks in advance.

You will need to send Authorisation header with Bearer token;

Authorization: Bearer <access_token>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM