使用现有的访问令牌在.net中请求谷歌驱动器
Using existing access token for google drive request in .net
在我的网站上,我希望用户能够将他们创建的文件上传到谷歌驱动器。该文件位于服务器上。
目前,我正在使用javascript对用户进行身份验证并接收访问令牌。然后我使用ajax将访问令牌发布到我的服务器。
不过,我的问题是在googledrive.netsdkv3中使用这个访问令牌。
Dim Service = New DriveService()
如何告知服务使用我获得的accesstoken?它似乎需要一种身份验证。
目前的流量在facebook和youtube上运行良好。
更新:
现在发送直接消息或获取响应时发生获取错误。
我运行了fiddler,得到的响应是"error":"redirect_uri_mismatch",所以我更改了AuthState.Callback=New uri(NativeApplicationClient.OutOfBandCallbackUrl)以反映我当前的域,但仍然出现了该错误。我已确保应用程序控制台中的应用程序设置正确。
我认为这是因为我在javascript中发送的重定向uri与我在.net中发送的不同。javascript回调url是"postmessage"
我试过不发送回拨网址,但它抱怨它不见了。尝试设置一个回调url"postmessage",但它抱怨它不是uri。
有什么想法吗?
如果你看一下C#快速入门,你可以看到控制台应用程序创建了你使用JS发出的请求,并要求用户写回AuthToken。
这里有一个类似于我用来获取DriveService对象的VB代码
Imports System
Imports System.Diagnostics
Imports System.Configuration
Imports DotNetOpenAuth.OAuth2
Imports Google.Apis.Authentication.OAuth2
Imports Google.Apis.Authentication.OAuth2.DotNetOpenAuth
Imports Google.Apis.Drive.v2
Imports Google.Apis.Drive.v2.Data
Imports Google.Apis.Util
Imports Google.Apis.Services
Private Shared CLIENT_ID As String
Private Shared CLIENT_SECRET As String
Private Shared AUTH_CODE As String
Private Shared FOLDER_NAME As String
Private Shared FolderId As String
Private Shared oProvider As NativeApplicationClient
Private Shared oAuth As OAuth2Authenticator(Of NativeApplicationClient)
Private Shared oDriveService As DriveService
Private Shared Sub Initialize(nAuthCode as string)
If CLIENT_ID Is Nothing Or CLIENT_SECRET Is Nothing Or AUTH_CODE Is Nothing Then
'MSO - 20130423 - Read from the config the CLIENT_ID and the Secret if they are empty
CLIENT_ID = ConfigurationManager.AppSettings("CLIENT_ID")
CLIENT_SECRET = ConfigurationManager.AppSettings("CLIENT_SECRET")
AUTH_CODE = nAuthCode
'Auth Provider
oProvider = New NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET)
'Create OAuth2 autentication using the previously generated Auth Key
oAuth = New OAuth2Authenticator(Of NativeApplicationClient)(oProvider, AddressOf GetAuthorization)
'Initialize the DriveService Object
Dim oBasicInit As New BaseClientService.Initializer()
oBasicInit.Authenticator = oAuth
oDriveService = New DriveService(oBasicInit)
End If
End Sub
Private Shared Function GetAuthorization(arg As NativeApplicationClient) As IAuthorizationState
'Auth Scope (in this case Google Drive Read Only)
Dim AuthState As IAuthorizationState = New AuthorizationState({DriveService.Scopes.DriveReadonly.GetStringValue()})
AuthState.Callback = New Uri(NativeApplicationClient.OutOfBandCallbackUrl)
AuthState = arg.ProcessUserAuthorization(AUTH_CODE, AuthState)
Return AuthState
End Function
代码应该可以工作,让我知道是否有任何问题
相关文章:
- ASP.NET WebMethod将整个页面返回给JQuery ajax请求
- Node.js HTTP/NET——连接和请求之间的区别
- 对服务器的 GET 请求返回 net::ERR_CONNECTION_REFUSED
- 在设置用户后,从 JavaScript JSON 请求中获取 Web 服务 (asmx) ASP.NET Windows
- HUE API JavaScript HTTP PUT请求返回;net::ERR_EMPTY_REPONSE”;
- ASP.Net MVC 5 Ajax 请求在编辑页面上不存在错误__RequestVerificationToken
- 如何在服务器 ASP.NET 上请求客户端 Cookie
- asp.net MVC 的客户端验证“潜在危险的请求表单...”
- 需要在文本框中允许“<”和“>”,而不设置验证请求=fale ASP.net
- AJAX 可访问的长时间运行的服务任务阻止在启用 ASP.NET 兼容性/会话的环境中的后续 AJAX 服务请求
- 类型错误:在 ASP.NET 中触发 AJAX 请求时,未定义不是一个函数
- ASP.NET 对同步 AJAX 请求的 MVC 操作
- 返回 base64 文件的字符串作为响应 ASP.NET 使用 httpHandler 对 AJAX 请求的响应
- Javascript 无法解析正确的图像路径,当使用 URL 路由在 ASP.Net 中请求页面时
- ASP.NET MVC ActionResult如何返回数据以响应GET请求
- NodeJS如何在net模块中实现请求/响应
- jquery ajax 请求 asp.net 代码隐藏不起作用
- 运行jQuery脚本发出ajax请求2分钟后出现错误net::ERR_INSUFFICIENT_RESOURCES
- ASP.Net MVC:HTTP错误414.请求URL太长
- 查找请求是通过Script或. net请求