Google Drive API身份验证

Anu*_*bis 12 google-drive-api google-api-dotnet-client

我想创建一个可以随时访问我自己的Google云端硬盘的应用程序,在那里创建文件,共享它们等等.根据https://developers.google.com/drive/service-accounts "使用常规Google帐户作为应用程序拥有的帐户"我认为我需要的唯一一步是获取access_token和refresh_token一次,将它们存储在我的应用程序中并使用refresh_token I可以刷新我的access_token(不知何故).

我可以使用https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/drive.file&redirect_uri=http://localhost;response_type=等请求获取access_token 令牌CLIENT_ID =

在用户对话框中批准此应用程序请求后,我将被重定向到我的localhost,我将获得在3600秒内到期的access_token.

问题是:

1.如何获得refresh_token?
2.如何使用refresh_token刷新access_token?

我不想使用Google的API客户端库,因为它很糟糕(.NET).

Anu*_*bis 19

好,我知道了.答案可以在这里找到:https://developers.google.com/accounts/docs/OAuth2WebServer#offline

首先你必须提出一个Auth请求

    <form method="POST" action="https://accounts.google.com/o/oauth2/auth">
        <input type="hidden" name="scope" value="[YOUR SCOPE]"/>
        <input type="hidden" name="client_id" value="[YOUR CLIENT ID]"/>
        <input type="hidden" name="response_type" value="code"/>
        <input type="hidden" name="redirect_uri" value="[YOUR RETURN URL]"/>
        <input type="hidden" name="access_type" value="offline"/>
        <input type="submit"/>
    </form>
Run Code Online (Sandbox Code Playgroud)

然后,您将获得return_url的"代码"

然后,您需要将代码交换为access_token和refresh_token

        <form method="POST" action="https://accounts.google.com/o/oauth2/token">
            <input type="text" name="code" value="[CODE YOU GOT IN PREV STEP]"/>
            <input type="hidden" name="client_id" value="[YOUR CLIENT ID]"/>
            <input type="hidden" name="client_secret" value="YOUR CLIENT SECRET"/>
            <input type="hidden" name="grant_type" value="authorization_code"/>
            <input type="hidden" name="redirect_uri" value="YOUR REDIRECT URL"/>
            <input type="submit"/>
        </form>
Run Code Online (Sandbox Code Playgroud)

因此,您会打赌回复如下:

{
  "access_token" : "[HERE YOU ACCESS TOKEN]",
  "token_type" : "Bearer",
  "expires_in" : 3600,
  "id_token" : "eyJhbGciOiJSUzI1NiIsImtpZCI6ImRiMjBlNWMwZGU1YWI0MGRjNTU5ODBkM2EzYmZlNDdlOGM2NGM5YjAifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiY2lkIjoiMjQ2ODg5NjU3NDg2LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiYXVkIjoiMjQ2ODg5NjU3NDg2LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwidG9rZW5faGFzaCI6IjRURGtlQ0MzVWRPZHoyd2k1N2RnaUEiLCJpZCI6IjExNTI0MDk1NDM0Njg1NTU4NjE2MSIsImlhdCI6MTM1MzQwNDQ3MCwiZXhwIjoxMzUzNDA4MzcwfQ.Va98sh9LvMEIWxpRMFkcuFqtDAUfJLN5M__oJyjvmIxQR9q2NUIoocyjqbNyXc7as_ePQYiUjajx0SCumtR4Zhv-exeJfrKA_uMmJTe7jWhK6K2R3JQ2-aIZNnehpEuhYZBXgLhzYz1mlFrLqQTdV6LjDhRPDH-ol4UKWXfbAVE",
  "refresh_token" : "[HERE YOUR REFRESH TOKEN]"
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以在应用程序中存储这些令牌,并且可以无限制地使用每隔3600秒刷新access_token

            <form method="POST" action="https://accounts.google.com/o/oauth2/token">
                <input type="text" name="refresh_token" value="[YOUR REFRESH TOKEN]"/>
                <input type="hidden" name="client_id" value="[YOUR CLIENT ID]"/>
                <input type="hidden" name="client_secret" value="[YOUR CLIENT SECRET]"/>
                <input type="hidden" name="grant_type" value="refresh_token"/>
                <input type="submit"/>
            </form>
Run Code Online (Sandbox Code Playgroud)

每次发出此请求,您都会获得一个新的access_token

{
  "access_token" : "[NEW ACCESS TOKEN]",
  "token_type" : "Bearer",
  "expires_in" : 3600,
  "id_token" : "eyJhbGciOiJSUzI1NiIsImtpZCI6ImRiMjBlNWMwZGU1YWI0MGRjNTU5ODBkM2EzYmZlNDdlOGM2NGM5YjAifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiYXVkIjoiMjQ2ODg5NjU3NDg2LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwidG9rZW5faGFzaCI6ImpyYk5oNkRHZFN4Y0w5MUI5Q1hab2ciLCJpZCI6IjExNTI0MDk1NDM0Njg1NTU4NjE2MSIsImNpZCI6IjI0Njg4OTY1NzQ4Ni5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsImlhdCI6MTM1MzQwNTU5OSwiZXhwIjoxMzUzNDA5NDk5fQ.mGN3EYOX75gPubr3TqWIOBkfq-o3JBXMXx4MbxEBGMSuPdJi7VTqZa4isyR-st-J5_wTtA-j8tVQYnDeZDxj5KpJ14FFQPKTtv_VI5kvuT55KyOmGu4yidciYoffJMISisr8NqiksbemaiYX900sRv6PmoTA6Nf6VtHgj3BZjWo"
}
Run Code Online (Sandbox Code Playgroud)