Tro*_*y G 19 javascript c# asp.net youtube-api google-oauth
我一直在一个网站上为用户上传视频到共享的YouTube帐户以供日后访问.经过大量的工作,我已经能够获得一个主动令牌和可行的刷新令牌.
但是,初始化YouTubeService对象的代码如下所示:
UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
// This OAuth 2.0 access scope allows an application to upload files to the
// authenticated user's YouTube channel, but doesn't allow other types of access.
new[] { YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None
);
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name,
});
Run Code Online (Sandbox Code Playgroud)
我已经有了一个令牌,我想用我的.我正在使用ASP.NET 3.5版,所以async无论如何我都无法打电话.
有没有办法可以在YouTubeService没有async调用的情况下创建一个对象,并使用我自己的令牌?有没有一种方法可以在没有授权代理的情况下构建凭证对象?
或者,该应用程序使用YouTube API V2已有一段时间了,并且有一个带有令牌的表单,并针对与API V2中的令牌一起生成的YouTube URI执行了后续操作.有没有办法用V3实现?有没有办法使用Javascript上传视频,可能还有我可以在我的代码中使用的示例?
注意:我最终将框架升级到 4.5 以访问 google 库。
要以编程方式初始化 UserCredential 对象,您必须构建 Flow 和 TokenResponse。流程需要一个范围(也就是我们为凭证寻求的权限)。
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Responses;
using Google.Apis.Auth.OAuth2.Flows;
string[] scopes = new string[] {
YouTubeService.Scope.Youtube,
YouTubeService.Scope.YoutubeUpload
};
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = XXXXXXXXXX, <- Put your own values here
ClientSecret = XXXXXXXXXX <- Put your own values here
},
Scopes = scopes,
DataStore = new FileDataStore("Store")
});
TokenResponse token = new TokenResponse {
AccessToken = lblActiveToken.Text,
RefreshToken = lblRefreshToken.Text
};
UserCredential credential = new UserCredential(flow, Environment.UserName, token);
Run Code Online (Sandbox Code Playgroud)
希望有帮助。
| 归档时间: |
|
| 查看次数: |
751 次 |
| 最近记录: |