Google API-无效的凭证 [英] Google API - Invalid Credentials

查看:76
本文介绍了Google API-无效的凭证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个iOS应用,用户可以使用 Google登录来对我的身份进行身份验证用户.我遵循了实施指南,似乎用户登录.我可以访问用户的数据,例如他们的个人资料图像,姓名等.

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.

因为我使用的是 YouTube API ,所以我需要在用户登录之前将YouTube范围添加到Google登录中.因此,我这样做是...

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"]

现在,每当用户登录时,它都会请求访问其YouTube频道.

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

似乎一切正常.但是,现在要对YouTube视频进行评分.您可以查看 Google有关如何对YouTube视频进行评分的文档,但基本上,这就是我要做的.

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)")
        }

现在,因为这需要身份验证,所以API需要访问令牌.因此(如您在上面看到的),我为API提供了Google登录所提供的访问令牌.就是:

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-HIgJv83RHXhGNKdVkWn0sEn7XtaWhTbeVjg8hsE2K

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!

推荐答案

尝试将访问令牌添加到您的Alamofire请求的标头中,而不是将其作为参数传递.因此:

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)

这篇关于Google API-无效的凭证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆