Google API - Invalid Credentials(Google API - 凭据无效)
问题描述
我正在尝试制作一个 iOS 应用,用户可以在其中使用
似乎一切都运行良好.但是,现在要评价 YouTube 视频.您可以查看 Google 关于如何评价 YouTube 视频的文档,但基本上,这是我必须做的.
Alamofire.request("https://www.googleapis.com/youtube/v3/videos/rate", 方法: .post, 参数: ["access_token":token, "id":"9T56NNzHE7A","rating":"like"], encoding: URLEncoding.default, headers: nil).responseJSON { (response) in打印((响应)")}
现在因为这需要身份验证,所以 API 需要一个访问令牌.所以(如您在上面看到的)我给 API 提供了谷歌登录给我的访问令牌.即:
let token = GIDSignIn.sharedInstance().currentUser.authentication.accessToken!
但是,请求不起作用.我得到的回应是
成功:{错误 = {代码 = 401;错误 = ({域=全局;位置=授权;位置类型 = 标题;message = "无效的凭证";原因 = authError;});message = "无效的凭证";};}
我不明白如果用户仅在几秒钟前登录,凭据会如何无效.如果有帮助,这里是我的访问令牌的示例(不要担心这实际上不是它):
<块引用>ve35.LmaIBHsK3dUPNR34rb0fgThwiZj-dNB7k935EhyVK1X8nkgMBmA-_3Hxhys7uk-HEm3ggg-HIgJv83RHXhGNKdVkWn0sEn7XtaWhTbeVjg8hsBK3hK8H1Gx8KzhhaEJGVg
我一直在互联网上寻找答案,但无济于事.所以我决定问问.有谁知道如何解决这个问题?任何意见将不胜感激!
尝试将访问令牌添加到 Alamofire
请求的标头中,而不是将其作为参数传递.因此:
let headers: HTTPHeaders = ["Authorization": "Bearer (token)"]让请求= Alamofire.request(路径,方法:方法,参数:参数,编码:JSONEncoding.default,标头:标头)
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.
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!
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 - 凭据无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Google API - 凭据无效
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- URL编码Swift iOS 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- UITextView 内容插图 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01