我正在尝试对YouTube Data API v3进行授权调用,如下所述: https://developers.google.com/youtube/v3/docs/activities/list
我得到了一切正常,直到我到达需要访问令牌的部分。
我添加了GoogleID登录,它为我提供了具有以下范围的访问令牌:
"https://www.googleapis.com/auth/youtube"
"https://www.googleapis.com/auth/youtube.force-ssl"
"https://www.googleapis.com/auth/youtube.readonly"
这些是三个范围,在谷歌的参考文献中提到并使用。
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()
电话结束后,我得到了以下答案:
{
error = {
code = 401;
errors = (
{
domain = global;
location = Authorization;
locationType = header;
message = "Invalid Credentials";
reason = authError;
}
);
message = "Invalid Credentials";
};
}
我已经尝试使用URL字符串中的access_token进行调用:
theURLString = "https://www.googleapis.com/youtube/v3/activities?part=snippet,contentDetails&home=true&maxResults=10&key=\(googleAPIKey)&access_token=\(Settings.sharedInstance.userToken!)"
并获得以下内容:
{
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.";
};
}
编辑1:
找到Google OAuth游乐场: https://developers.google.com/oauthplayground/
从该游乐场获取访问令牌并将其硬编码添加到代码中不会改变两种情况下的响应。