本文介绍了在.net中对谷歌驱动器请求使用现有访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上,我希望用户能够将他们创建的文件上传到Google Drive。该文件位于服务器上。



目前我正在使用javascript来验证用户身份并接收访问令牌。然后,我使用ajax将访问令牌发布到我的服务器上。



我的问题是使用此访问令牌与谷歌驱动器.net sdk v3。

  Dim Service =新DriveService()

我如何告诉服务使用我已经获得的使用权限?它似乎需要一种Iauthenticate。



目前的流程对于脸书和YouTube而言效果良好。

更新:

现在在发送直接信息或获取时发生错误我跑了fiddler和响应是错误:redirect_uri_mismatch,所以我改变了AuthState.Callback =新的Uri(NativeApplicationClient.OutOfBandCallbackUrl)来反映我的目前的域名,但仍然得到这个错误。我认为它是因为我用javascript发送的重定向URI与我发送的不同。净。 Javascript的回调网址是postmessage

我曾尝试不发送回电话的URL,但它抱怨它缺少。
尝试设置一个postmessage的回调url,但它抱怨说它不是一个uri。



有什么想法?

解决方案

如果您查看,您可以看到控制台应用程序创建使用JS所做的请求,并要求用户写回AuthToken。



这里有一个VB代码类似于我用来获取DriveService对象的类型

$ $ p $ 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
导入Google.Apis.Drive.v2.Data
导入Google.Apis.Util
导入Google.Apis.Services

私有共享CLIENT_ID作为字符串
私人共享CLIENT_SECRET作为字符串
私有共享AUTH_CODE作为字符串
私有共享FOLDER_NAME作为字符串
私有共享FolderId作为字符串

私有共享oProvider作为NativeApplicationClient
私有共享oAuth作为OAuth2Authenticator(Of NativeApplicationClient)
私有共享oDriveService作为DriveService
Private Shared Sub初始化(nAuthCode as字符串)

如果CLIENT_ID是Nothing或CLIENT_SECRET是Nothing或AUTH_CODE是Nothing Then
'MSO - 20130423 - 从配置中读取CLIENT_ID和秘密(如果它们为空)

CLIENT_ID = ConfigurationManager.AppSettings(CLIENT_ID)
CLIENT_SECRET = ConfigurationManager.AppSettings( CLIENT_SECRET)
AUTH_CODE = nAuthCode
'Auth Provider
oProvider =新的NativeApplicationClient(GoogleAuthenticationServer.Description,CLIENT_I D,CLIENT_SECRET)

'使用先前生成的认证密钥创建OAuth2认证
oAuth =新的OAuth2Authenticator(Of NativeApplicationClient)(oProvider,AddressOf GetAuthorization)

'Initialize DriveService Object
Dim oBasicInit As New BaseClientService.Initializer()
oBasicInit.Authenticator = oAuth
oDriveService = New DriveService(oBasicInit)

End If

End Sub

私有共享函数GetAuthorization(arg As NativeApplicationClient)作为IAuthorizationState


'Auth Scope(本例中为Google Drive Read Only)
Dim AuthState As IAuthorizationState = New AuthorizationState({DriveService.Scopes.DriveReadonly.GetStringValue()})
AuthState.Callback =新的Uri(NativeApplicationClient.OutOfBandCallbackUrl)
AuthState = arg.Proce ssUserAuthorization(AUTH_CODE,AuthState)
返回AuthState
End Function

代码应该工作让我知道是否有任何问题

On my website i would like the user to be able to upload a file they have created to google drive. The file is located on the server.

Currently i am using javascript to authenticate the user and receive an access token. I am then using ajax to post the access token to the my server.

My trouble though is using this access token with the google drive .net sdk v3.

Dim Service = New DriveService()

How do I tell the service to using the accesstoken i have acquired? It seems to require a type of Iauthenticate.

The current flow is working well for facebook and youtube.

Update:

Now getting Error occurred while sending a direct message or getting the response.

I ran fiddler and the response was "error" : "redirect_uri_mismatch" so i changed the AuthState.Callback = New Uri(NativeApplicationClient.OutOfBandCallbackUrl) to reflect my current domain but still get that error. I have made sure the app is setup correct in the app console.

I think its because the redirect uri i am sending in javascript is different to the one I am sending in .net. Javascripts callback url is "postmessage"

I have tried not sending a call back url but it complains it is missing.Have tried setting a callback url of "postmessage" but it complains that it is not a uri.

Any ideas?

解决方案

If you take a look at the C# Quick start you can see the console app creating the request you made using JS and asking the user to write back the AuthToken.

Here a VB code similar to the one I use to get the DriveService object

    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

The code should work let me know if there is any problem

这篇关于在.net中对谷歌驱动器请求使用现有访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 00:56