- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
是否有任何使用 OpenID 和 OAuth 的 youtube API(或 gdata API)的 c# 代码示例?
或者可能是一篇带有代码示例的帖子/文章
将 dotnetoauth 与 youtube API 结合使用的代码示例也很棒
最佳答案
在过去的 2 周中,我刚刚获得了与 YouTube 的 Oauth 连接以获得访问播放列表的授权,我可以向您发送大量有用的类(class)。我可以在这里发布部分内容,但它可以让我发布更多内容!
虽然我现在可以访问私有(private)供稿,但我无法使用 OAuth 上传视频。谷歌文档说它有效,但最后实际发布的 SDK 是去年 2 月。您可以下载源代码并编译它,但我仍然遇到问题并构建了自己的类。我最终求助于 ClientLogin 来上传视频,但其他一切都求助于 OAuth...
给我留言,告诉我你的电子邮件地址和你想要达到的目标,我会把我现在写的类(class)发给你。
这里是一些代码:首先是一个类,它执行 OAuth 签名和一些其他的东西,比如创建 header :
#Region "Imports"
Imports System.Text
Imports System.Security.Cryptography
#End Region
Public NotInheritable Class SharedOAuth
#Region "Class Declarations"
Private Const unreservedChars As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~"
Private Const OAuthVersion As String = "1.0"
Private Const OAuthParameterPrefix As String = "oauth_"
Private Const OAuthParameterExclusionPrefix As String = "xoauth_"
Private Const OAuthConsumerKeyKey As String = "oauth_consumer_key"
Private Const OAuthCallbackKey As String = "oauth_callback"
Private Const OAuthVersionKey As String = "oauth_version"
Private Const OAuthSignatureMethodKey As String = "oauth_signature_method"
Private Const OAuthSignatureKey As String = "oauth_signature"
Private Const OAuthTimestampKey As String = "oauth_timestamp"
Private Const OAuthNonceKey As String = "oauth_nonce"
Private Const OAuthTokenKey As String = "oauth_token"
Private Const OAuthTokenSecretKey As String = "oauth_token_secret"
#End Region
#Region "Constructor"
Private Sub New()
'prevent construction
End Sub
#End Region
#Region "Shared Functions"
Public Shared Function GetSignatureTypeNameFromType(ByVal signatureType As enumerations.OAuthEnumerations.SignatureTypes) As String
Select Case signatureType
Case enumerations.OAuthEnumerations.SignatureTypes.HmacSha1
Return "HMAC-SHA1"
Case Else
Return String.Empty
End Select
End Function
Public Shared Function GenerateTimeStamp() As String
'Implementation of UNIX current UTC time
Dim _ts As TimeSpan = DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0, 0)
Return Convert.ToInt64(_ts.TotalSeconds).ToString(CultureInfo.InvariantCulture)
End Function
Public Shared Function GenerateNonce() As String
'Some random Number
Return New Random().Next(123400, 9999999).ToString
End Function
Public Shared Function UrlEncode(ByVal value As String) As String
Dim _result As New StringBuilder
If (String.IsNullOrEmpty(value)) Then
Return value
End If
For Each _symbol As Char In value
If unreservedChars.IndexOf(_symbol) <> -1 Then
_result.Append(_symbol)
Else
_result.Append(String.Format("%{0}", String.Format("{0:X2}", Asc(_symbol))))
End If
Next
Return _result.ToString
End Function
''' <summary>
'''Generates a signature using the specified signatureType
''' </summary>
''' <param name="url">The full url that needs to be signed including its non OAuth url parameters</param>
''' <param name="consumerKey">The consumer key</param>
''' <param name="consumerSecret">The consumer seceret</param>
''' <param name="token">The token, if available. If not available pass null or an empty string</param>
''' <param name="tokenSecret">The token secret, if available. If not available pass null or an empty string</param>
''' <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param>
''' <param name="signatureType">The type of signature to use</param>
''' <returns>A base64 string of the hash value</returns>
Public Shared Function GenerateSignature(ByVal url As Uri, ByVal consumerKey As String, ByVal consumerSecret As String, ByVal token As String, ByVal tokenSecret As String, ByVal httpMethod As String, ByVal timeStamp As String, ByVal nonce As String, ByVal signatureType As enumerations.OAuthEnumerations.SignatureTypes, ByRef normalizedUrl As String, ByRef normalizedRequestParameters As String) As String
normalizedUrl = String.Empty
normalizedRequestParameters = String.Empty
Dim _signatureBase As String
Dim _hash As HMACSHA1
Select Case signatureType
Case enumerations.OAuthEnumerations.SignatureTypes.HmacSha1
_signatureBase = GenerateSignatureBase(url, consumerKey, token, tokenSecret, httpMethod, timeStamp, nonce, enumerations.OAuthEnumerations.SignatureTypes.HmacSha1, normalizedUrl, normalizedRequestParameters)
Dim _sb As New StringBuilder
With _sb
.Append(UrlEncode(consumerSecret))
.Append("&")
If (String.IsNullOrEmpty(tokenSecret)) Then
.Append("")
Else
.Append(UrlEncode(tokenSecret))
End If
End With
_hash = New HMACSHA1
_hash.Key = Encoding.ASCII.GetBytes(_sb.ToString)
Return GenerateSignatureUsingHash(_signatureBase, _hash)
Case Else
Throw New NotImplementedException
End Select
End Function
Public Shared Function GenerateSignatureUsingHash(ByVal signatureBase As String, ByVal hash As HashAlgorithm) As String
Return ComputeHash(hash, signatureBase)
End Function
Public Shared Function GenerateSignatureBase(ByVal url As Uri, ByVal consumerKey As String, ByVal token As String, ByVal tokenSecret As String, ByVal httpMethod As String, ByVal timeStamp As String, ByVal nonce As String, ByVal signatureType As enumerations.OAuthEnumerations.SignatureTypes, ByRef normalizedUrl As String, ByRef normalizedRequestParameters As String) As String
Dim _SignatureTypeName As String = GetSignatureTypeNameFromType(signatureType)
If String.IsNullOrEmpty(token) Then
token = String.Empty
End If
If String.IsNullOrEmpty(tokenSecret) Then
tokenSecret = String.Empty
End If
If String.IsNullOrEmpty(consumerKey) Then
Throw New ArgumentNullException("consumerKey")
End If
If String.IsNullOrEmpty(httpMethod) Then
Throw New ArgumentNullException("httpMethod")
End If
If String.IsNullOrEmpty(_SignatureTypeName) Then
Throw New ArgumentNullException("SignatureType")
End If
normalizedUrl = String.Empty
normalizedRequestParameters = String.Empty
Dim _params As List(Of QueryParameter) = getqueryparameters(url.Query)
With _params
.Add(New QueryParameter(OAuthVersionKey, OAuthVersion))
.Add(New QueryParameter(OAuthNonceKey, nonce))
.Add(New QueryParameter(OAuthTimestampKey, timeStamp))
.Add(New QueryParameter(OAuthSignatureMethodKey, _SignatureTypeName))
.Add(New QueryParameter(OAuthConsumerKeyKey, consumerKey))
If Not (String.IsNullOrEmpty(token)) Then
.Add(New QueryParameter(OAuthTokenKey, token))
End If
.Sort(New QueryParameterComparer)
End With
normalizedUrl = String.Format("{0}://{1}", url.Scheme, url.Host)
If Not ((url.Scheme = "http" AndAlso url.Port = 80) OrElse (url.Scheme = "https" AndAlso url.Port = 443)) Then
normalizedUrl = String.Format("{0}:{1}", normalizedUrl, url.Port)
End If
normalizedUrl = String.Format("{0}{1}", normalizedUrl, url.AbsolutePath)
normalizedRequestParameters = NormalizeRequestParameters(_params)
Dim _sb As New StringBuilder
With _sb
.AppendFormat(CultureInfo.InvariantCulture, "{0}&", httpMethod.ToUpper)
.AppendFormat(CultureInfo.InvariantCulture, "{0}&", UrlEncode(normalizedUrl))
.AppendFormat(CultureInfo.InvariantCulture, "{0}", UrlEncode(normalizedRequestParameters))
End With
Return _sb.ToString
End Function
#End Region
#Region "Private Methods"
Private Shared Function ComputeHash(ByVal hashAlgorithm As HashAlgorithm, ByVal data As String) As String
If hashAlgorithm Is Nothing Then
Throw New ArgumentNullException("hashAlgorithm")
End If
If String.IsNullOrEmpty(data) Then
Throw New ArgumentNullException("data")
End If
Dim _dataBuffer As Byte() = Encoding.ASCII.GetBytes(data)
Dim _hashBytes As Byte() = hashAlgorithm.ComputeHash(_dataBuffer)
Return Convert.ToBase64String(_hashBytes)
End Function
Private Shared Function NormalizeRequestParameters(ByVal parameters As IList(Of QueryParameter)) As String
Dim _p As QueryParameter
Dim _sb As New StringBuilder
For i As Integer = 0 To parameters.count - 1
_p = parameters(i)
_sb.AppendFormat(CultureInfo.InvariantCulture, "{0}={1}", _p.Name, _p.Value)
If i < parameters.count - 1 Then
_sb.Append("&")
End If
Next
Return _sb.ToString
End Function
Private Shared Function GetQueryParameters(ByVal parameters As String) As List(Of QueryParameter)
If (parameters.StartsWith("?")) Then
parameters = parameters.Remove(0, 1)
End If
Dim _result As New List(Of QueryParameter)
Dim _p As String() = parameters.Split("&"c)
Dim _temp As String()
If Not (String.IsNullOrEmpty(parameters)) Then
For Each s As String In _p
If Not (String.IsNullOrEmpty(s)) AndAlso Not (s.StartsWith(OAuthParameterExclusionPrefix)) Then 'AndAlso (s.StartsWith(OAuthParameterPrefix)) Then
If s.IndexOf("=") > -1 Then
_temp = s.Split("="c)
_result.Add(New QueryParameter(_temp(0), _temp(1)))
Else
_result.Add(New QueryParameter(s, String.Empty))
End If
End If
Next
End If
Return _result
End Function
#End Region
End Class
然后,我使用这是一个包装类来帮助简化调用:
Public Function SignCommandUrl(ByVal commandUrl As String) As String
Return SignCommandUrl(commandUrl, Nothing, Nothing)
End Function
Public Function SignCommandUrl(ByVal commandUrl As String, ByVal overrideToken As String, ByVal overrideTokenSecret As String) As String
Dim _commandUri = New Uri(commandUrl)
Dim _oAuthSignature As String
Dim _oAuthTimeStamp As String = SharedOAuth.GenerateTimeStamp
Dim _oAuthNOnce As String = SharedOAuth.GenerateNonce
Dim _oAuth_normalized_url As String = String.Empty
Dim _oAuth_normalized_params As String = String.Empty
If Not (String.IsNullOrEmpty(overrideToken)) OrElse Not (String.IsNullOrEmpty(overrideTokenSecret)) Then
_oAuthSignature = SharedOAuth.GenerateSignature(_commandUri, CONSUMER_KEY, CONSUMER_SECRET, overrideToken, overrideTokenSecret, "GET", _oAuthTimeStamp, _oAuthNOnce, enumerations.OAuthEnumerations.SignatureTypes.HmacSha1, _oAuth_normalized_url, _oAuth_normalized_params)
Else
If MasterAccessToken Is Nothing Then
_oAuthSignature = SharedOAuth.GenerateSignature(_commandUri, CONSUMER_KEY, CONSUMER_SECRET, String.Empty, String.Empty, "GET", _oAuthTimeStamp, _oAuthNOnce, enumerations.OAuthEnumerations.SignatureTypes.HmacSha1, _oAuth_normalized_url, _oAuth_normalized_params)
Else
_oAuthSignature = SharedOAuth.GenerateSignature(_commandUri, CONSUMER_KEY, CONSUMER_SECRET, SharedOAuth.UrlEncode(MasterAccessToken.Token), MasterAccessToken.TokenSecret, "GET", _oAuthTimeStamp, _oAuthNOnce, enumerations.OAuthEnumerations.SignatureTypes.HmacSha1, _oAuth_normalized_url, _oAuth_normalized_params)
End If
End If
'Get a token
Dim _sb As New System.Text.StringBuilder
With _sb
.Append(_oAuth_normalized_url)
.Append("?")
.Append(_oAuth_normalized_params)
.Append("&")
.Append("oauth_signature")
.Append("=")
.Append(SharedOAuth.UrlEncode(_oAuthSignature))
End With
Return _sb.ToString
End Function
Public Function BuildPostHeader(ByVal commandUrl As String) As String
Dim _commandUri = New Uri(commandUrl)
Dim _oAuthTimeStamp As String = SharedOAuth.GenerateTimeStamp
Dim _oAuthNOnce As String = SharedOAuth.GenerateNonce
Dim _oAuth_normalized_url As String = String.Empty
Dim _oAuth_normalized_params As String = String.Empty
Dim _oAuthSignature As String
If MasterAccessToken Is Nothing Then
_oAuthSignature = SharedOAuth.GenerateSignature(_commandUri, CONSUMER_KEY, CONSUMER_SECRET, String.Empty, String.Empty, "POST", _oAuthTimeStamp, _oAuthNOnce, enumerations.OAuthEnumerations.SignatureTypes.HmacSha1, _oAuth_normalized_url, _oAuth_normalized_params)
Else
_oAuthSignature = SharedOAuth.GenerateSignature(_commandUri, CONSUMER_KEY, CONSUMER_SECRET, SharedOAuth.UrlEncode(MasterAccessToken.Token), MasterAccessToken.TokenSecret, "POST", _oAuthTimeStamp, _oAuthNOnce, enumerations.OAuthEnumerations.SignatureTypes.HmacSha1, _oAuth_normalized_url, _oAuth_normalized_params)
End If
Dim _sb As New System.Text.StringBuilder
With _sb
.Append("Authorization: OAuth oauth_version=""1.0"", ")
.AppendFormat("oauth_nonce=""{0}"", ", _oAuthNOnce)
.AppendFormat("oauth_timestamp=""{0}"", ", _oAuthTimeStamp)
.AppendFormat("oauth_consumer_key=""{0}"", ", CONSUMER_KEY)
.AppendFormat("oauth_token=""{0}"", ", MasterAccessToken.Token)
.Append("oauth_signature_method=""HMAC-SHA1"", ")
.AppendFormat("oauth_signature=""{0}""", _oAuthSignature)
End With
Return _sb.ToString
End Function
Public Shared Function PostRequestReturnCollection(ByVal commandUrl As String) As NameValueCollection
Dim _nameValCollection As NameValueCollection
Dim _request As HttpWebRequest = CType(WebRequest.Create(commandUrl), HttpWebRequest)
Using _response As HttpWebResponse = CType(_request.GetResponse, HttpWebResponse)
Using _reader As TextReader = New StreamReader(_response.GetResponseStream)
_nameValCollection = HttpUtility.ParseQueryString(_reader.ReadToEnd)
End Using
End Using
Return _nameValCollection
End Function
关于使用 OpenID 和 OAuth 的 youtube API(或 gdata API)的 c# 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2501885/
OpenID 是否改善了用户体验? 编辑 不是要贬低其他评论,但我在下面得到了一个非常好的回复,它以一种合理的底线方式概述了 OpenID 的 3 个优点。我还在其他评论中听到了一些耳语,你可以通过
我不想疏远我的用户,但是通过多种方式登录网站违背了实现 OpenID 的目的。这不是一个全新的网站,大约有 3000 名用户是顽固的(我们有一个很棒的社区),但并非所有人都是顽固的,我不想因为登录过程
我在 StackOverflow 上使用 OpenID 来验证我的用户身份,这与这里的使用方式非常相似。我真正需要做的是让 OpenID 在我网站的所有子域中工作。 该站点的行为与 Kijiji 大致
OpenID 有没有其他选择,我的意思是一个提供我们详细信息的站点,我们可以使用该 ID 登录到一个站点? 更新 假设认为我正在开发一个具有 openID 集成的网站,我将只有一个用户名,没有那么多细
有人可以帮助我了解 OpenID 的工作原理吗?我对以下答案感兴趣: 如果使用 OpenId,您还需要存储用户 ID 和密码吗? 当用户登录时,我的应用程序如何创建新 session ? 当用户退出应
最重要的问题是我的电子邮件地址是否被传输到消费服务。 例如,如果我使用 Google 在这里登录 SO,那么 SO 知道我的 gmail 地址吗? 他知道我在 gmail 设置中输入的用于外发邮件的名
OpenID 连接的当前状态是什么?我想将它用于新的 SSO 系统。是否有任何库可用于实现? 最佳答案 最终 OpenID Connect 规范于 2014 年 2 月 26 日根据 http://o
作为单点登录实现,我认为 OpenID 很棒。即便如此,它是否一定是电子商务的好选择?我知道它可以使用,但应该使用吗?您是否冒险将所有访问详细信息放在一个篮子中? 那里的普遍意见是什么? 最佳答案 当
我不是问具体的实现,也不是问跨站单点登录机制的全局世界观,我只是想知道社区对 OpenID 底层可用性的看法。您是否认为使用由(非技术观察者)随机提供的各种提供者发布的 URL 来代替实际的用户名是人
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭11 年前。 Improve th
我是openID的新手。我花了很多时间思考最好的做法是为用户提供选择,并使用各种启用了OpenID的帐户登录。 (我必须进一步澄清,我的系统不是一个只需要简单的“一次性身份验证”即可进行博客发布的系统
我希望将 OpenID 实现为一组合作伙伴网站的提供商。问题是这个网站是为 child (13 岁以下)准备的,所以我有一些业务限制需要处理 - 主要是帐户不能用于任何非合作伙伴网站(我们与每个获得批
WordPress 的 OpenID 插件似乎不接受任何 Google OpenID 提供商链接: http://google.com/profiles/username 或 https://goog
我在看 DotNetOpenAuth samples并且有两个 OpenID 提供程序示例;和 OpenID 提供程序和一个 OpenID Web Ring SSO 提供程序。 有谁知道两者之间的区别
存在一个行为不当的 OpenID Connect“兼容”iDP(它现在应该保持无名)——它在使用范围 openid 和任何包含 id_token 的 response_type 时抛出错误。这肯定是一
有没有人知道或有任何我可以用来构建使用 OpenID 的站点的文档?例如,当用户访问我的网站时,我接受一个 OpenID,然后我会将他们重定向到 OpenID 提供商,然后当他们通过身份验证时,他们将
现在我使用 3rd 方网站作为我的 openid 提供者(myvidoop 和 myopenid)。我正在考虑让我的网站充当我的提供者。我认为肯定会有一些脚本可以安装并轻松实现。我已经尝试过 janr
我有用户帐户的本地表 用户 ID(nvarchar)、密码、电子邮件、姓氏角色等。 现在每个子表中都使用了 UserID。我想在我的站点中启用 OPENID 注册,稍后将集成本地用户注册功能。我应该将
我遇到了 google openid 标识符的问题。 我在简单的 asp.net mvc 项目中使用 dotnetopenauth 库(dotnetopenid 的继承者)。 在本地主机上的测试期间,
我对 OpenId 很陌生,并且在身份验证完成后理解如何使用 OpenId 时遇到了一些问题。 我正在创建一个新站点,并且在使 openId 身份验证正常工作方面没有问题。但是我不确定一旦用户登录,我
我是一名优秀的程序员,十分优秀!