Kullanıcının oturumunu açın

Bu, Classroom eklentilerindeki adım adım açıklamalı ikinci kılavuzdur. size yol gösterir.

Bu adım adım açıklamalı kılavuzda, web uygulamasına Google ile Oturum Açma özelliğini ekleyeceksiniz. Bu, için gerekli olan davranış olup olmadığını kontrol edin. Şu adresteki kimlik bilgilerini kullan: bu yetkilendirme akışını devre dışı bırakır.

Bu adım adım açıklamalı kılavuz kapsamında aşağıdaki işlemleri tamamlayacaksınız:

  • Web uygulamanızı, iFrame içindeki oturum verilerini koruyacak şekilde yapılandırın.
  • Google OAuth 2.0 sunucudan sunucuya oturum açma akışını uygulayın.
  • OAuth 2.0 API'ye bir çağrı gönderin.
  • Yetkilendirme, oturum kapatma ve test işlemlerini destekleyecek ek rotalar oluşturun API çağrıları.

Tamamladığınızda web uygulamanızda kullanıcıları tam olarak yetkilendirebilir ve Google API'leri

Yetkilendirme akışını anlama

Google API'leri, kimlik doğrulama ve yetkilendirme için OAuth 2.0 protokolünü kullanır. Google'ın OAuth uygulamasının tam açıklamasını şurada bulabilirsiniz: Google Identity OAuth kılavuzu.

Uygulamanızın kimlik bilgileri Google Cloud'da yönetilir. Bunlar başarılı bir şekilde bir kullanıcı kimliğini doğrulamak ve yetkilendirmek için dört adımlı bir kullanıcı:

  1. Yetkilendirme isteyin. Bu isteğin bir parçası olarak bir geri çağırma URL'si sağlayın. Tamamlandığında bir yetkilendirme URL'si alırsınız.
  2. Kullanıcıyı yetkilendirme URL'sine yönlendirin. Açılan sayfa, uygulamanızın gerektirdiği izinlerin kullanıcısı ve erişime izin vermesini ister. İşlem tamamlandığında, kullanıcı geri çağırma URL'sine yönlendirilir.
  3. Geri arama rotanızda bir yetkilendirme kodu alın. erişim jetonu ve yenileme jetonu için yetkilendirme kodu.
  4. Jetonları kullanarak bir Google API'sine çağrı yapmak.

OAuth 2.0 kimlik bilgilerini alma

OAuth kimlik bilgilerini şu sayfada açıklandığı şekilde oluşturup indirdiğinizden emin olun: genel bakış sayfasına gidin. Projenizde kullanıcının oturum açmak için bu kimlik bilgilerini kullanması gerekir.

Yetkilendirme akışını uygulama

Açıklanan akışı gerçekleştirmek için web uygulamamıza mantık ve rota ekleyin. Şunlar da dahildir: şu özellikler:

  • Açılış sayfasına eriştikten sonra yetkilendirme akışını başlatın.
  • Yetkilendirme isteyin ve yetkilendirme sunucusu yanıtını işleyin.
  • Depolanmış kimlik bilgilerini temizleyin.
  • Uygulamanın izinlerini iptal edin.
  • API çağrısı test etme.

Yetkilendirmeyi başlat

Gerekirse açılış sayfanızı yetkilendirme akışını başlatacak şekilde değiştirin. İlgili içeriği oluşturmak için kullanılan eklenti iki olası durumda olabilir; veya tablodaki kayıtlı jetonlar veya OAuth 2.0 sunucusundan jeton almanız gerekir. Gerçekleştirme Oturumda jeton varsa bir test API çağrısı varsa veya kullanıcıya başka şekilde istem gönderdiyse tıklayın.

Python

routes.py dosyanızı açın. İlk olarak birkaç sabit değer ve çerezimizi iframe güvenlik önerilerine göre yapılandırmanız gerekir.

# The file that contains the OAuth 2.0 client_id and client_secret.
CLIENT_SECRETS_FILE = "client_secret.json"

# The OAuth 2.0 access scopes to request.
# These scopes must match the scopes in your Google Cloud project's OAuth Consent
# Screen: https://console.cloud.google.com/apis/credentials/consent
SCOPES = [
    "openid",
    "https://www.googleapis.com/auth/userinfo.profile",
    "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/classroom.addons.teacher",
    "https://www.googleapis.com/auth/classroom.addons.student"
]

# Flask cookie configurations.
app.config.update(
    SESSION_COOKIE_SECURE=True,
    SESSION_COOKIE_HTTPONLY=True,
    SESSION_COOKIE_SAMESITE="None",
)

Eklenti açılış rotanıza gidin (örnekte /classroom-addon kullanılmaktadır) dosyası) ekleyebilirsiniz. Oturum, oturum açma sayfasını şunu içermiyorsa bir oturum açma sayfası oluşturmak için mantık ekleyin: "kimlik bilgileri" tuşuna basın.

@app.route("/classroom-addon")
def classroom_addon():
    if "credentials" not in flask.session:
        return flask.render_template("authorization.html")

    return flask.render_template(
        "addon-discovery.html",
        message="You've reached the addon discovery page.")

Java

Bu adım adım açıklamalı kılavuzun kodunu step_02_sign_in modülünde bulabilirsiniz.

application.properties dosyasını açın ve iframe güvenlik önerilerine uyar.

# iFrame security recommendations call for cookies to have the HttpOnly and
# secure attribute set
server.servlet.session.cookie.http-only=true
server.servlet.session.cookie.secure=true

# Ensures that the session is maintained across the iframe and sign-in pop-up.
server.servlet.session.cookie.same-site=none

Hizmet sınıfı oluşturma (step_02_sign_in modülünde AuthService.java) uç noktaların arkasındaki mantığı işler ve yönlendirme URI'si, istemci gizli anahtarları dosya konumu ve eklentinizin gerekiyor. Yönlendirme URI'si, kullanıcılarınızı belirli bir URI'ye yeniden yönlendirmek için kullanılır izin vermelidir. Bkz. Proje Kurulumu README.md client_secret.json dosya

@Service
public class AuthService {
    private static final String REDIRECT_URI = "https://localhost:5000/callback";
    private static final String CLIENT_SECRET_FILE = "client_secret.json";
    private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
    private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();

    private static final String[] REQUIRED_SCOPES = {
        "https://www.googleapis.com/auth/userinfo.profile",
        "https://www.googleapis.com/auth/userinfo.email",
        "https://www.googleapis.com/auth/classroom.addons.teacher",
        "https://www.googleapis.com/auth/classroom.addons.student"
    };

    /** Creates and returns a Collection object with all requested scopes.
    *   @return Collection of scopes requested by the application.
    */
    public static Collection<String> getScopes() {
        return new ArrayList<>(Arrays.asList(REQUIRED_SCOPES));
    }
}

Denetleyici dosyasını açın (AuthController.java, step_02_sign_in modülünü) kullanarak oturum açma sayfasının oluşturulması için açılış rotasına bir mantık ekleyin. oturum credentials anahtarını içermez.

@GetMapping(value = {"/start-auth-flow"})
public String startAuthFlow(Model model) {
    try {
        return "authorization";
    } catch (Exception e) {
        return onError(e.getMessage(), model);
    }
}

@GetMapping(value = {"/addon-discovery"})
public String addon_discovery(HttpSession session, Model model) {
    try {
        if (session == null || session.getAttribute("credentials") == null) {
            return startAuthFlow(model);
        }
        return "addon-discovery";
    } catch (Exception e) {
        return onError(e.getMessage(), model);
    }
}

Yetkilendirme sayfanızda, kullanıcının "imzalayabileceği" bir bağlantı veya düğme inç" seçeneğini tıklayın. Bu düğme tıklandığında kullanıcı authorize yönlendirmesine yönlendirilir.

Yetkilendirme isteyin

Yetkilendirme istemek için kullanıcıyı yapılandırıp bir kimlik doğrulamasına yönlendirin URL'yi tıklayın. Bu URL'de kapsamlar gibi çeşitli bilgiler yer alır. istenen, sonra yetkilendirme için hedef rota ve web uygulamasının girin. Bunları bu örnek yetkilendirme URL'sinde görebilirsiniz.

Python

routes.py dosyanıza aşağıdaki içe aktarmayı ekleyin.

import google_auth_oauthlib.flow

Yeni bir rota oluşturun: /authorize. Şunun bir örneğini oluştur: google_auth_oauthlib.flow.Flow; dahil edilmiş olan Bunun için from_client_secrets_file yöntemini kullanın.

@app.route("/authorize")
def authorize():
    # Create flow instance to manage the OAuth 2.0 Authorization Grant Flow
    # steps.
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        CLIENT_SECRETS_FILE, scopes=SCOPES)

flow öğesinin redirect_uri değerini ayarlayın; bu, kullanıcıların gitmesini istediğiniz rotadır uygulamanızı yetkilendirdikten sonra geri dönen Bu şurada /callback: örneğine bakalım.

# The URI created here must exactly match one of the authorized redirect
# URIs for the OAuth 2.0 client, which you configured in the API Console. If
# this value doesn't match an authorized URI, you will get a
# "redirect_uri_mismatch" error.
flow.redirect_uri = flask.url_for("callback", _external=True)

authorization_url ve state oluşturmak için akış nesnesini kullanın. Mağaza oturumdaki state; Bu bilgiler, bilgilerin orijinalliğinin doğrulanması için yanıt vermesi gerekir. Son olarak, kullanıcıyı authorization_url

authorization_url, state = flow.authorization_url(
    # Enable offline access so that you can refresh an access token without
    # re-prompting the user for permission. Recommended for web server apps.
    access_type="offline",
    # Enable incremental authorization. Recommended as a best practice.
    include_granted_scopes="true")

# Store the state so the callback can verify the auth server response.
flask.session["state"] = state

# Redirect the user to the OAuth authorization URL.
return flask.redirect(authorization_url)

Java

Örneklendirmek için AuthService.java dosyasına aşağıdaki yöntemleri ekleyin: akış nesnesini görüntüler ve ardından, yetkilendirme URL'sini almak için bunu kullanın:

  • getClientSecrets() yöntemi, istemci gizli anahtarı dosyasını okur ve yapıları GoogleClientSecrets nesnesi.
  • getFlow() yöntemi, GoogleAuthorizationCodeFlow öğesinin bir örneğini oluşturur.
  • authorize() yöntemi GoogleAuthorizationCodeFlow nesnesini kullanır, state parametresini ve yetkilendirme URL'sini almak için yönlendirme URI'si ekleyin. Yanıtın doğruluğunu doğrulamak için state parametresi kullanılır sunucu. Yöntem, daha sonra yetkilendirme URL'sini ve state parametresini kullanın.
/** Reads the client secret file downloaded from Google Cloud.
 *   @return GoogleClientSecrets read in from client secret file. */
public GoogleClientSecrets getClientSecrets() throws Exception {
    try {
        InputStream in = SignInApplication.class.getClassLoader()
            .getResourceAsStream(CLIENT_SECRET_FILE);
        if (in == null) {
            throw new FileNotFoundException("Client secret file not found: "
                +   CLIENT_SECRET_FILE);
        }
        GoogleClientSecrets clientSecrets = GoogleClientSecrets
            .load(JSON_FACTORY, new InputStreamReader(in));
        return clientSecrets;
    } catch (Exception e) {
        throw e;
    }
}

/** Builds and returns authorization code flow.
*   @return GoogleAuthorizationCodeFlow object used to retrieve an access
*   token and refresh token for the application.
*   @throws Exception if reading client secrets or building code flow object
*   is unsuccessful.
*/
public GoogleAuthorizationCodeFlow getFlow() throws Exception {
    try {
        GoogleAuthorizationCodeFlow authorizationCodeFlow =
            new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT,
                JSON_FACTORY,
                getClientSecrets(),
                getScopes())
                .setAccessType("offline")
                .build();
        return authorizationCodeFlow;
    } catch (Exception e) {
        throw e;
    }
}

/** Builds and returns a map with the authorization URL, which allows the
*   user to give the app permission to their account, and the state parameter,
*   which is used to prevent cross site request forgery.
*   @return map with authorization URL and state parameter.
*   @throws Exception if building the authorization URL is unsuccessful.
*/
public HashMap authorize() throws Exception {
    HashMap<String, String> authDataMap = new HashMap<>();
    try {
        String state = new BigInteger(130, new SecureRandom()).toString(32);
        authDataMap.put("state", state);

        GoogleAuthorizationCodeFlow flow = getFlow();
        String authUrl = flow
            .newAuthorizationUrl()
            .setState(state)
            .setRedirectUri(REDIRECT_URI)
            .build();
        String url = authUrl;
        authDataMap.put("url", url);

        return authDataMap;
    } catch (Exception e) {
        throw e;
    }
}

Oluşturucu yerleştirmeyi kullanarak denetleyici sınıfı.

/** Declare AuthService to be used in the Controller class constructor. */
private final AuthService authService;

/** AuthController constructor. Uses constructor injection to instantiate
*   the AuthService and UserRepository classes.
*   @param authService the service class that handles the implementation logic
*   of requests.
*/
public AuthController(AuthService authService) {
    this.authService = authService;
}

/authorize uç noktasını denetleyici sınıfına ekleyin. Bu uç nokta şunu çağırır: state parametresini almak için AuthService authorize() yöntemini ve yetkilendirme URL'si. Ardından, uç nokta state öğesini depolar. parametresini belirtir ve kullanıcıları yetkilendirme URL'sine yönlendirir.

/** Redirects the sign-in pop-up to the authorization URL.
*   @param response the current response to pass information to.
*   @param session the current session.
*   @throws Exception if redirection to the authorization URL is unsuccessful.
*/
@GetMapping(value = {"/authorize"})
public void authorize(HttpServletResponse response, HttpSession session)
    throws Exception {
    try {
        HashMap authDataMap = authService.authorize();
        String authUrl = authDataMap.get("url").toString();
        String state = authDataMap.get("state").toString();
        session.setAttribute("state", state);
        response.sendRedirect(authUrl);
    } catch (Exception e) {
        throw e;
    }
}

Sunucu yanıtını işleme

Yetki verildikten sonra kullanıcı şuradan redirect_uri rotasına geri döner: önceki adım. Yukarıdaki örnekte, bu rota /callback şeklindedir.

Kullanıcıcode yetkilendirme sayfası. Ardından, kodu erişim ve yenileme jetonlarıyla değiştirin:

Python

Aşağıdaki içe aktarmaları Flask sunucusu dosyanıza ekleyin.

import google.oauth2.credentials
import googleapiclient.discovery

Yolu sunucunuza ekleyin. Farklı bir google_auth_oauthlib.flow.Flow, ancak bu kez önceki adım.

@app.route("/callback")
def callback():
    state = flask.session["state"]

    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        CLIENT_SECRETS_FILE, scopes=SCOPES, state=state)
    flow.redirect_uri = flask.url_for("callback", _external=True)

Ardından, erişim izni isteyin ve jetonları yenileyin. Neyse ki flow nesnesi de , bunu başarmak için fetch_token yöntemini içerir. Yöntemde code veya authorization_response bağımsız değişkenlerinden birini seçin. Şunu kullanın: authorization_response, istekteki tam URL'dir.

authorization_response = flask.request.url
flow.fetch_token(authorization_response=authorization_response)

Artık tüm kimlik bilgilerine sahipsiniz. Oturumda depolayabilmeleri için diğer yöntemlerde veya rotalarda alınıp bir eklentiye yönlendirilebilir. açılış sayfası.

credentials = flow.credentials
flask.session["credentials"] = {
    "token": credentials.token,
    "refresh_token": credentials.refresh_token,
    "token_uri": credentials.token_uri,
    "client_id": credentials.client_id,
    "client_secret": credentials.client_secret,
    "scopes": credentials.scopes
}

# Close the pop-up by rendering an HTML page with a script that redirects
# the owner and closes itself. This can be done with a bit of JavaScript:
# <script>
#     window.opener.location.href = "{{ url_for('classroom_addon') }}";
#     window.close();
# </script>
return flask.render_template("close-me.html")

Java

Hizmet sınıfınıza şu şekilde Credentials nesnesini döndüren bir yöntem ekleyin: tarafından gerçekleştirilen yönlendirmeden alınan yetkilendirme kodunun yetkilendirme URL'si. Bu Credentials nesnesi daha sonra almak için kullanılır erişim jetonunu ve yenileme jetonunu ekleyin.

/** Returns the required credentials to access Google APIs.
*   @param authorizationCode the authorization code provided by the
*   authorization URL that's used to obtain credentials.
*   @return the credentials that were retrieved from the authorization flow.
*   @throws Exception if retrieving credentials is unsuccessful.
*/
public Credential getAndSaveCredentials(String authorizationCode) throws Exception {
    try {
        GoogleAuthorizationCodeFlow flow = getFlow();
        GoogleClientSecrets googleClientSecrets = getClientSecrets();
        TokenResponse tokenResponse = flow.newTokenRequest(authorizationCode)
            .setClientAuthentication(new ClientParametersAuthentication(
                googleClientSecrets.getWeb().getClientId(),
                googleClientSecrets.getWeb().getClientSecret()))
            .setRedirectUri(REDIRECT_URI)
            .execute();
        Credential credential = flow.createAndStoreCredential(tokenResponse, null);
        return credential;
    } catch (Exception e) {
        throw e;
    }
}

Yönlendirme URI'niz için denetleyiciye bir uç nokta ekleyin. Al yetkilendirme kodu ve istekten state parametresi alınır. Bunu karşılaştır state parametresini oturumda depolanan state özelliğine eklemeniz gerekir. Şu durumda: eşleşmesi ve ardından yetkilendirme akışına devam etmeniz gerekir. Eşleşmezlerse bir hata döndürür.

Ardından, AuthService getAndSaveCredentials yöntemini çağırın ve yetkilendirme kodunu kullanabilirsiniz. Credentials öğesini aldıktan sonra bu dosyayı oturumda depolayın. Ardından iletişim kutusunu kapatın ve kullanıcıyı eklenti açılış sayfasına yönlendirme

/** Handles the redirect URL to grant the application access to the user's
*   account.
*   @param request the current request used to obtain the authorization code
*   and state parameter from.
*   @param session the current session.
*   @param response the current response to pass information to.
*   @param model the Model interface to pass error information that's
*   displayed on the error page.
*   @return the close-pop-up template if authorization is successful, or the
*   onError method to handle and display the error message.
*/
@GetMapping(value = {"/callback"})
public String callback(HttpServletRequest request, HttpSession session,
    HttpServletResponse response, Model model) {
    try {
        String authCode = request.getParameter("code");
        String requestState = request.getParameter("state");
        String sessionState = session.getAttribute("state").toString();
        if (!requestState.equals(sessionState)) {
            response.setStatus(401);
            return onError("Invalid state parameter.", model);
        }
        Credential credentials = authService.getAndSaveCredentials(authCode);
        session.setAttribute("credentials", credentials);
        return "close-pop-up";
    } catch (Exception e) {
        return onError(e.getMessage(), model);
    }
}

API çağrısı test etme

Akış tamamlandıktan sonra artık Google API'lerine çağrı gönderebilirsiniz.

Örneğin, kullanıcının profil bilgilerini isteyebilirsiniz. Şunu isteyebilirsiniz: OAuth 2.0 API'daki kullanıcı bilgileri.

Python

Şuna ilişkin dokümanları okuyun: OAuth 2.0 keşif API'si Doldurulan bir UserInfo nesnesini almak için bunu kullanın.

# Retrieve the credentials from the session data and construct a
# Credentials instance.
credentials = google.oauth2.credentials.Credentials(
    **flask.session["credentials"])

# Construct the OAuth 2.0 v2 discovery API library.
user_info_service = googleapiclient.discovery.build(
    serviceName="oauth2", version="v2", credentials=credentials)

# Request and store the username in the session.
# This allows it to be used in other methods or in an HTML template.
flask.session["username"] = (
    user_info_service.userinfo().get().execute().get("name"))

Java

Hizmet sınıfında, şunu kullanarak UserInfo nesnesi oluşturan bir yöntem oluşturun: Credentials kullanabilirsiniz.

/** Obtains the Userinfo object by passing in the required credentials.
*   @param credentials retrieved from the authorization flow.
*   @return the Userinfo object for the currently signed-in user.
*   @throws IOException if creating UserInfo service or obtaining the
*   Userinfo object is unsuccessful.
*/
public Userinfo getUserInfo(Credential credentials) throws IOException {
    try {
        Oauth2 userInfoService = new Oauth2.Builder(
            new NetHttpTransport(),
            new GsonFactory(),
            credentials).build();
        Userinfo userinfo = userInfoService.userinfo().get().execute();
        return userinfo;
    } catch (Exception e) {
        throw e;
    }
}

/test uç noktasını kullanıcının e-posta adresini gösteren denetleyiciye ekleyin.

/** Returns the test request page with the user's email.
*   @param session the current session.
*   @param model the Model interface to pass error information that's
*   displayed on the error page.
*   @return the test page that displays the current user's email or the
*   onError method to handle and display the error message.
*/
@GetMapping(value = {"/test"})
public String test(HttpSession session, Model model) {
    try {
        Credential credentials = (Credential) session.getAttribute("credentials");
        Userinfo userInfo = authService.getUserInfo(credentials);
        String userInfoEmail = userInfo.getEmail();
        if (userInfoEmail != null) {
            model.addAttribute("userEmail", userInfoEmail);
        } else {
            return onError("Could not get user email.", model);
        }
        return "test";
    } catch (Exception e) {
        return onError(e.getMessage(), model);
    }
}

Kimlik bilgilerini temizle

Buradaki bilgileri kullanıcının kimlik bilgilerini geçerli oturumdan kaldırabilirsiniz. Bu, eklenti açılış sayfasındaki yönlendirmeyi test etmenize olanak tanır.

Kullanıcının daha önce oturumu kapattığına dair bir bildirim göstermenizi öneririz veya eklenti açılış sayfasına yönlendirir. Uygulamanız yetkilendirme akışı, yeni kimlik bilgilerinin alınması için gereklidir, ancak kullanıcılardan uygulamanızı yeniden yetkilendirebilirsiniz.

Python

@app.route("/clear")
def clear_credentials():
    if "credentials" in flask.session:
        del flask.session["credentials"]
        del flask.session["username"]

    return flask.render_template("signed-out.html")

Alternatif olarak flask.session.clear() kullanın, ancak bu, istenmeyen bir işlem olabilir etkisini gösterir.

Java

Denetleyicide bir /clear uç noktası ekleyin.

/** Clears the credentials in the session and returns the sign-out
*   confirmation page.
*   @param session the current session.
*   @return the sign-out confirmation page.
*/
@GetMapping(value = {"/clear"})
public String clear(HttpSession session) {
    try {
        if (session != null && session.getAttribute("credentials") != null) {
            session.removeAttribute("credentials");
        }
        return "sign-out";
    } catch (Exception e) {
        return onError(e.getMessage(), model);
    }
}

Uygulamanın iznini iptal etme

Bir kullanıcı, şuna bir POST isteği göndererek uygulamanızın iznini iptal edebilir: https://oauth2.googleapis.com/revoke. İstek, kullanıcının erişim jetonu.

Python

import requests

@app.route("/revoke")
def revoke():
    if "credentials" not in flask.session:
        return flask.render_template("addon-discovery.html",
                            message="You need to authorize before " +
                            "attempting to revoke credentials.")

    credentials = google.oauth2.credentials.Credentials(
        **flask.session["credentials"])

    revoke = requests.post(
        "https://oauth2.googleapis.com/revoke",
        params={"token": credentials.token},
        headers={"content-type": "application/x-www-form-urlencoded"})

    if "credentials" in flask.session:
        del flask.session["credentials"]
        del flask.session["username"]

    status_code = getattr(revoke, "status_code")
    if status_code == 200:
        return flask.render_template("authorization.html")
    else:
        return flask.render_template(
            "index.html", message="An error occurred during revocation!")

Java

Hizmet sınıfına, iptal uç noktasına çağrı yapan bir yöntem ekleyin.

/** Revokes the app's permissions to the user's account.
*   @param credentials retrieved from the authorization flow.
*   @return response entity returned from the HTTP call to obtain response
*   information.
*   @throws RestClientException if the POST request to the revoke endpoint is
*   unsuccessful.
*/
public ResponseEntity<String> revokeCredentials(Credential credentials) throws RestClientException {
    try {
        String accessToken = credentials.getAccessToken();
        String url = "https://oauth2.googleapis.com/revoke?token=" + accessToken;

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
        HttpEntity<Object> httpEntity = new HttpEntity<Object>(httpHeaders);
        ResponseEntity<String> responseEntity = new RestTemplate().exchange(
            url,
            HttpMethod.POST,
            httpEntity,
            String.class);
        return responseEntity;
    } catch (RestClientException e) {
        throw e;
    }
}

Denetleyiciye, oturumu temizleyen bir uç nokta (/revoke) ekleyin ve İptal işlemi yapılmışsa kullanıcıyı yetkilendirme sayfasına yönlendirirse işidir.

/** Revokes the app's permissions and returns the authorization page.
*   @param session the current session.
*   @return the authorization page.
*   @throws Exception if revoking access is unsuccessful.
*/
@GetMapping(value = {"/revoke"})
public String revoke(HttpSession session) throws Exception {
    try {
        if (session != null && session.getAttribute("credentials") != null) {
            Credential credentials = (Credential) session.getAttribute("credentials");
            ResponseEntity responseEntity = authService.revokeCredentials(credentials);
            Integer httpStatusCode = responseEntity.getStatusCodeValue();

            if (httpStatusCode != 200) {
                return onError("There was an issue revoking access: " +
                    responseEntity.getStatusCode(), model);
            }
            session.removeAttribute("credentials");
        }
        return startAuthFlow(model);
    } catch (Exception e) {
        return onError(e.getMessage(), model);
    }
}

Eklentiyi test etme

Google Classroom'da oturum açın. Öğretmen test kullanıcılarınızdan biri olarak. Sınıf Çalışmaları sekmesine gidin ve yeni bir Ödev oluşturun. Metin alanının altındaki Eklentiler düğmesini tıklayın, ve eklentinizi seçin. iframe açılır ve eklenti GWM SDK'sının Uygulaması'nda belirttiğiniz Ek Kurulum URI'si Yapılandırma sayfasını ziyaret edin.

Tebrikler! Sonraki adıma geçmeye hazırsınız: işlem tekrarı ziyaret edin.