- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试创建一个在指定 channel 上传视频的应用程序,而不提示登录页面。我会尝试更好地解释我需要什么。
我正在使用适用于 REST 的 Objective-C 的 Google API 客户端库,有了这个库,我可以使用“标准”上传流程:
用户录制视频 -> 他按下上传按钮 -> Safari 打开登录谷歌页面 -> 用户登录他自己的帐户并授予应用程序权限 -> Safari 重定向回 ios 应用程序 -> 上传过程开始 -> 视频将上传到个人用户 channel 。
相反,这是我的 ios 应用程序所需的工作流程:
用户录制视频 -> 他按下上传按钮 -> 视频将上传到应用的 youtube channel 。
我找到的唯一帮助是这个 article ,它解释了一种获取可刷新应用程序 token 以在应用程序 channel 中上传视频的方法。这正是我所需要的。无论如何,这是一个网络示例,它上传服务器中的视频。我的视频在手机里,所以我想我必须这样修改这篇文章的流程:
通过以 channel 所有者身份登录首次获取 token -> 创建一个 token.txt 并将其保存在我的服务器中 -> 创建一个名为 get_token.php 的页面,打印 token.txt 的内容并在 token 过期时刷新它.
在我的应用程序中有了这个流程,我需要另一个流程:
用户录制视频 -> 按上传按钮 -> 我调用了 get_token.php 并检索了实际的 token -> 我通过库调用了检索到的 token ,以将视频上传到应用程序的 youtube channel 。
这里我发现了一些问题,这是我的身份验证方法:
#pragma mark - Sign In
- (void)authNoCodeExchange {
[self verifyConfig];
NSURL *issuer = [NSURL URLWithString:kIssuer];
[self logMessage:@"Fetching configuration for issuer: %@", issuer];
// discovers endpoints
[OIDAuthorizationService discoverServiceConfigurationForIssuer:issuer
completion:^(OIDServiceConfiguration *_Nullable configuration, NSError *_Nullable error) {
if (!configuration) {
[self logMessage:@"Error retrieving discovery document: %@", [error localizedDescription]];
return;
}
[self logMessage:@"Got configuration: %@", configuration];
if (!kClientID) {
[self doClientRegistration:configuration
callback:^(OIDServiceConfiguration *configuration,
OIDRegistrationResponse *registrationResponse) {
[self doAuthWithoutCodeExchange:configuration
clientID:registrationResponse.clientID
clientSecret:registrationResponse.clientSecret];
}];
} else {
[self doAuthWithoutCodeExchangeCri:configuration clientID:kClientID clientSecret:nil];
}
}];
}
/////////////////
- (void)doAuthWithoutCodeExchangeCri:(OIDServiceConfiguration *)configuration
clientID:(NSString *)clientID
clientSecret:(NSString *)clientSecret {
NSURL *redirectURI = [NSURL URLWithString:kRedirectURI];
OIDTokenRequest *tokenExchangeRequest =
[_authState.lastAuthorizationResponse tokenExchangeRequest];
[OIDAuthorizationService performTokenRequest:tokenExchangeRequest
callback:^(OIDTokenResponse *_Nullable tokenResponse,
NSError *_Nullable error) {
if (!tokenResponse) {
[self logMessage:@"Token exchange error: %@", [error localizedDescription]];
} else {
[self logMessage:@"Received token response with accessToken: %@", tokenResponse.accessToken];
}
[_authState updateWithTokenResponse:tokenResponse error:error];
GTMAppAuthFetcherAuthorization *gtmAuthorization =
[[GTMAppAuthFetcherAuthorization alloc] initWithAuthState:authState];
// Sets the authorizer on the GTLRYouTubeService object so API calls will be authenticated.
self.youTubeService.authorizer = gtmAuthorization;
// Serializes authorization to keychain in GTMAppAuth format.
[GTMAppAuthFetcherAuthorization saveAuthorization:gtmAuthorization
toKeychainForName:kGTMAppAuthKeychainItemName];
[self uploadVideoFile];
}];
}
我也有类似的东西:
NSString * RetrivedToken = @"ya29xxxxxxx3nJxxxxxxx6qqQ-FxxxxxxxdGH";
我如何修改这些方法以接受我的 retrivedtoken 而不是他们从标准身份验证工作流程中检索的方法?
最佳答案
这不是一个真正的答案,但它对于评论来说太重要了。
因此,您想要创建一个允许其他人上传到您的 youtube channel 的应用程序。通常我会说使用一个服务帐户,它可以让你更容易地做到这一点。但是 YouTube API 不支持服务帐户。
保存刷新 token 后,您将需要对应用进行身份验证,然后将此刷新 token 嵌入到您的应用中。
为什么这不是移动应用程序的好主意。
刷新 token 可能会过期,因此如果它确实过期或损坏,那么您将不得不再次对应用进行身份验证并将新的刷新 token 嵌入到您的应用中并向您的用户发布新版本。
另一种方法是设置一个带有刷新 token 的网络服务,并让您的应用访问该网络服务以进行上传。然后,如果刷新 token 损坏,您只需在您的网络服务上修复它。
在它变得困惑之前我已经这样做了,但真的没有其他方法可以做到这一点。
关于ios - 在用于 REST 的 Objective-C 的 Google API 客户端库中上传带有 "fixed" token 的 Youtube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45917122/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!