1

I am trying to make an iOS app where a user can rate a YouTube video using the YouTube API. To do this, the user has to be authenticated. So, I used Google's Google Sign-In to authenticate my users. I followed the implementation guide, and it seems as if the user gets signed in. I can access data from the user such as their profile image, name, etc.

Because I am using the YouTube API, I need to add the YouTube scope to Google Sign-In before the user signs in. So, I do that like so...

GIDSignIn.sharedInstance().scopes = ["https://www.googleapis.com/auth/youtube"]

Now whenever a user signs in, it will request access to their YouTube channel.

Screenshot of Google Sign-In page

It seems as if everything is working perfectly. But, now to rate the YouTube video. You can check out Google's documentation on how to rate a YouTube video, but basically, this is what I have to do.

Alamofire.request("https://www.googleapis.com/youtube/v3/videos/rate", method: .post, parameters: ["access_token":token, "id":"9T56NNzHE7A","rating":"like"], encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
            print("\(response)")
        }

Now because this requires authentication, the API needs an access token. So (as you can see above) I give the API the access token Google Sign-In gives me. Which is:

let token = GIDSignIn.sharedInstance().currentUser.authentication.accessToken!

But, the request does not work. The response I get is

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

I don't understand how the credentials could be invalid if the user signed in only seconds before. If it helps, here is an example of what my access token looks like (don't worry this isn't actually it):

ve35.LmaIBHsK3dUPNR34rb0fgThwiZj-dNB7k935EhyVK1X8nkgMBmA-_3Hxhys7uk-HEm3ggg-HIgJv83RHXhGNKdVkWn0sEn7XtaWhTbeVjg8hsBK3hK8H1Gx8KzhhaEJGVg

I've been looking all over the internet trying to find an answer to this but to no avail. So I decided to ask. Does anyone have any idea on how to fix this? Any input would be highly appreciated!

11
  • I am pretty sure the access token needs to be added to the headers following OAuth2 protocol and not passed as a parameter. See: developers.google.com/youtube/v3/guides/authentication Commented May 28, 2018 at 13:17
  • I am not an ios dev but you can pass the access toekn as part of the reeust you just add it &access_token=XXX or you send it as an authentication header bearer token. However the error message makes me think that there is something wrong with the access token you are sending are you 100% sure you have saved the latest and arent using an older one. Commented May 28, 2018 at 13:19
  • 1
    @DaImTo I didn't know that was possible. Commented May 28, 2018 at 13:25
  • 1
    @JacobCavin Alamofire has HTTPHeaders typealias which is just a [String:String] dictionary, so you can jsut do let headers: HTTPHeaders = ["Authorization": "Bearer \(token)"] and then pass to Alamofire request. Commented May 28, 2018 at 13:38
  • 1
    To follow up on my previous comment: let request = Alamofire.request(path, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: headers) Commented May 28, 2018 at 13:41

1 Answer 1

1

Try adding the access token to the headers of your Alamofire request instead of passing it as a parameter. As such:

let headers: HTTPHeaders = ["Authorization": "Bearer \(token)"]
let request = Alamofire.request(path, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.