尝试在OAuth 2.0中使用Youtube API时出现401 Invalid Credentials错误。
我使用google登录sdk并使用params获取access_token:
GIDSignIn *sharedSignIn = [GIDSignIn sharedInstance];
sharedSignIn.shouldFetchBasicProfile = NO;
sharedSignIn.scopes = [NSArray arrayWithObjects:
@"https://www.googleapis.com/auth/youtube.force-ssl",
@"https://www.googleapis.com/auth/youtube",
@"https://www.googleapis.com/auth/youtube.readonly",
// @"https://www.googleapis.com/auth/youtube.upload",
nil];
[sharedSignIn signIn];
比我使用AFNetworking库获取GET请求
- (void) getInformationWithParams: (NSDictionary *) params
method: (NSString *) method
onSuccess: (void(^)(NSDictionary *responseObject)) success
onFailure: (void (^) (NSError *error)) failure {
[self.requestOperationManager GET:method
parameters:params
progress:nil
success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
NSLog(@"%@", responseObject);
if (success) {
success(responseObject);
}
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"getInformationWithParams Error: %@", error);
if (failure) {
failure(error);
}
}];
}
之前我做过baseURL init
NSURL *baseURL = [NSURL URLWithString:@"https://www.googleapis.com/youtube/v3"];
self.requestOperationManager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
最后我通过
获得请求 NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
@"snippet", @"part",
@"true", @"home",
myApiKey, @"key",
myAccessToken, @"access_token",
nil];
[[SMServerManager sharedManager] getInformationWithParams:params
method:@"activities"
onSuccess:^(NSDictionary *responseObject) {
}
onFailure:^(NSError *error) {
}];
我不明白我做错了什么。
PS:请求工作,如果他们不需要使用acces_token。
答案 0 :(得分:0)
这是一个非常愚蠢的错误。我在几个谷歌帐户中感到困惑,并为项目制作了错误的文件。