virusss8 virusss8 - 5 months ago 23
Swift Question

iOS Youtube API v3 retrieve my channels

[IOS SWIFT]

I have a problem with youtube api v3, and I really hit a dead end

So, in my app I have newest GoogleSign-In implemented

let newScope: Array =
["https://www.googleapis.com/auth/youtube",
"https://www.googleapis.com/auth/youtube.force-ssl",
"https://www.googleapis.com/auth/youtube.readonly"]
GIDSignIn.sharedInstance().scopes = newScope


This shows how I add new scopes.

I successfully sign in user and retreive user id, it's data, access_token etc.

Now that the user is signedin, I need to get his youtubeId/channelId.

I did some research and I found that I can get my channel id with:
https://www.googleapis.com/youtube/v3/channels?part=id&mine=true&key={API_KEY_HERE}


I even tried to send access_token in url, but with no luck..

But something is wrong and I get 401 status code -


The request uses the
mine
parameter but is not properly
authorized.


What am I doing wrong?

Here I tested and everything works fine:
https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.channels.list


result here in sandbox:

{
"kind": "youtube#channelListResponse",
"etag": "\"XXXXXXX\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#channel",
"etag": "\"XXXXX\"",
"id": "XXXXX",
"googlePlusUserId": "ID_ID_ID"
}
}
]
}


Anyone have idea? solution? proposal?
Thankyou very much

In fact, why am I not authorized if I just successfully signedIn myself??

Tried even adding header for authorization:

Bearer {id_access_token}

Answer

I finally found the solution.

Yes, the header needs to be send. The only problem was with token!

When the signIn is successful, you get

GIDGoogleUser

instance.

That - lets call it "user", have .authentication with userID, idToken and accessToken

In documentation there is only mention of idToken, so I thought that this is actually the access token. How silly of me :/

Lesson learned!