How to pass an access_token to YouTube API v3?
Asked Answered
E

0

7

I'm trying to make an authorized call to YouTube Data API v3, as it's described here: https://developers.google.com/youtube/v3/docs/activities/list

I got everything working fine, until I get to the part, where the access token is needed.

I've added GoogleID Sign In, that gives me an access token with the following scopes:

"https://www.googleapis.com/auth/youtube"
"https://www.googleapis.com/auth/youtube.force-ssl"
"https://www.googleapis.com/auth/youtube.readonly"

Those are three scopes, that are mentioned and used at google's reference.

let url = NSURL(string: theURLString)

let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
let sessionQueue = NSOperationQueue.mainQueue()

let session = NSURLSession(configuration: sessionConfig, delegate: self, delegateQueue: sessionQueue)

let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

if Settings.sharedInstance.isLoggedIn {
    request.setValue("Bearer \(Settings.sharedInstance.userToken!)", forHTTPHeaderField: "Authorization")
}

let task = session.downloadTaskWithRequest(request)

task.resume()

And after the call I got the following answer:

{
error =     {
    code = 401;
    errors =         (
                    {
            domain = global;
            location = Authorization;
            locationType = header;
            message = "Invalid Credentials";
            reason = authError;
        }
    );
    message = "Invalid Credentials";
};
}

And I've already tried to make calls with the access_token in the URL string:

theURLString = "https://www.googleapis.com/youtube/v3/activities?part=snippet,contentDetails&home=true&maxResults=10&key=\(googleAPIKey)&access_token=\(Settings.sharedInstance.userToken!)"

And get the following:

{
error =     {
    code = 401;
    errors =         (
                    {
            domain = "youtube.parameter";
            location = home;
            locationType = parameter;
            message = "The request uses the <code>home</code> parameter but is not properly authorized.";
            reason = authorizationRequired;
        }
    );
    message = "The request uses the <code>home</code> parameter but is not properly authorized.";
};
}

EDIT 1:

Found Google OAuth playground: https://developers.google.com/oauthplayground/

Taking access token from that playground and adding it hard coded to the code doesn't change the response in both cases.

Euchologion answered 22/2, 2016 at 20:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.