অনুমোদন কলব্যাক হ্যান্ডলার তৈরি করুন

এই নথিটি ব্যাখ্যা করে যে কীভাবে একটি OAuth 2.0 অনুমোদন কলব্যাক হ্যান্ডলারকে একটি নমুনা ওয়েব অ্যাপ্লিকেশনের মাধ্যমে Java servlets ব্যবহার করে প্রয়োগ করতে হয় যা Google Tasks API ব্যবহার করে ব্যবহারকারীর কাজগুলি প্রদর্শন করবে৷ নমুনা অ্যাপ্লিকেশনটি প্রথমে ব্যবহারকারীর Google কার্যগুলি অ্যাক্সেস করার জন্য অনুমোদনের অনুরোধ করবে এবং তারপরে ডিফল্ট কার্য তালিকায় ব্যবহারকারীর কাজগুলি প্রদর্শন করবে৷

শ্রোতা

এই নথিটি জাভা এবং J2EE ওয়েব অ্যাপ্লিকেশন আর্কিটেকচারের সাথে পরিচিত ব্যক্তিদের জন্য তৈরি করা হয়েছে৷ OAuth 2.0 অনুমোদন প্রবাহ সম্পর্কে কিছু জ্ঞান সুপারিশ করা হয়।

বিষয়বস্তু

এই ধরনের সম্পূর্ণরূপে কাজ করার নমুনা পাওয়ার জন্য বেশ কয়েকটি পদক্ষেপ প্রয়োজন, আপনাকে এটি করতে হবে:

web.xml ফাইলে সার্লেট ম্যাপিং ঘোষণা করুন

আমরা আমাদের অ্যাপ্লিকেশনে 2টি সার্লেট ব্যবহার করব:

  • PrintTasksTitlesServlet ( / এ ম্যাপ করা হয়েছে): অ্যাপ্লিকেশনটির এন্ট্রি পয়েন্ট যা ব্যবহারকারীর প্রমাণীকরণ পরিচালনা করবে এবং ব্যবহারকারীর কাজগুলি প্রদর্শন করবে
  • OAuthCodeCallbackHandlerServlet ( /oauth2callback এ ম্যাপ করা হয়েছে): OAuth 2.0 কলব্যাক যা OAuth অনুমোদনের শেষ পয়েন্ট থেকে প্রতিক্রিয়া পরিচালনা করে

নীচে web.xml ফাইল রয়েছে যা এই 2টি সার্লেটকে আমাদের অ্যাপ্লিকেশনের URL-এ ম্যাপ করে:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <servlet>
   <servlet-name>PrintTasksTitles</servlet-name>
   <servlet-class>com.google.oauthsample.PrintTasksTitlesServlet</servlet-class>
 </servlet>

 <servlet-mapping>
   <servlet-name>PrintTasksTitles</servlet-name>
   <url-pattern>/</url-pattern>
 </servlet-mapping>

 <servlet>
   <servlet-name>OAuthCodeCallbackHandlerServlet</servlet-name>
   <servlet-class>com.google.oauthsample.OAuthCodeCallbackHandlerServlet</servlet-class>
 </servlet>

 <servlet-mapping>
   <servlet-name>OAuthCodeCallbackHandlerServlet</servlet-name>
   <url-pattern>/oauth2callback</url-pattern>
 </servlet-mapping>

</web-app>
/WEB-INF/web.xml ফাইল

আপনার সিস্টেমে ব্যবহারকারীদের প্রমাণীকরণ করুন এবং এর কার্যগুলি অ্যাক্সেস করার জন্য অনুমোদনের অনুরোধ করুন৷

ব্যবহারকারী রুট '/' URL এর মাধ্যমে অ্যাপ্লিকেশনটিতে প্রবেশ করে যা PrintTaskListsTitlesServlet servlet এ ম্যাপ করা হয়। সেই সার্লেটে নিম্নলিখিত কাজগুলি সম্পাদিত হয়:

  • ব্যবহারকারী সিস্টেমে প্রমাণীকৃত কিনা তা পরীক্ষা করে
  • ব্যবহারকারীর প্রমাণীকরণ না হলে তাকে প্রমাণীকরণ পৃষ্ঠায় পুনঃনির্দেশিত করা হয়
  • যদি ব্যবহারকারী প্রমাণীকৃত হয়, আমরা পরীক্ষা করি যে আমাদের ডেটা সঞ্চয়স্থানে ইতিমধ্যে একটি রিফ্রেশ টোকেন আছে কিনা - যা নীচের OAuthTokenDao দ্বারা পরিচালিত হয়৷ যদি ব্যবহারকারীর জন্য স্টোরে কোন রিফ্রেশ টোকেন না থাকে তবে এর অর্থ হল ব্যবহারকারী এখনও তার কার্যগুলি অ্যাক্সেস করার জন্য অ্যাপ্লিকেশন অনুমোদন দেয়নি। সেই ক্ষেত্রে ব্যবহারকারীকে Google-এর OAuth 2.0 অনুমোদনের শেষ পয়েন্টে পুনঃনির্দেশিত করা হয়।
নীচে এটি বাস্তবায়ন করার একটি উপায়:

package com.google.oauthsample;

import ...

/**
 * Simple sample Servlet which will display the tasks in the default task list of the user.
 */
@SuppressWarnings("serial")
public class PrintTasksTitlesServlet extends HttpServlet {

  /**
   * The OAuth Token DAO implementation, used to persist the OAuth refresh token.
   * Consider injecting it instead of using a static initialization. Also we are
   * using a simple memory implementation as a mock. Change the implementation to
   * using your database system.
   */
  public static OAuthTokenDao oauthTokenDao = new OAuthTokenDaoMemoryImpl();

  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // Getting the current user
    // This is using App Engine's User Service but you should replace this to
    // your own user/login implementation
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();

    // If the user is not logged-in it is redirected to the login service, then back to this page
    if (user == null) {
      resp.sendRedirect(userService.createLoginURL(getFullRequestUrl(req)));
      return;
    }

    // Checking if we already have tokens for this user in store
    AccessTokenResponse accessTokenResponse = oauthTokenDao.getKeys(user.getEmail());

    // If we don't have tokens for this user
    if (accessTokenResponse == null) {
      OAuthProperties oauthProperties = new OAuthProperties();
      // Redirect to the Google OAuth 2.0 authorization endpoint
      resp.sendRedirect(new GoogleAuthorizationRequestUrl(oauthProperties.getClientId(),
          OAuthCodeCallbackHandlerServlet.getOAuthCodeCallbackHandlerUrl(req), oauthProperties
              .getScopesAsString()).build());
      return;
    }
  }

  /**
   * Construct the request's URL without the parameter part.
   *
   * @param req the HttpRequest object
   * @return The constructed request's URL
   */
  public static String getFullRequestUrl(HttpServletRequest req) {
    String scheme = req.getScheme() + "://";
    String serverName = req.getServerName();
    String serverPort = (req.getServerPort() == 80) ? "" : ":" + req.getServerPort();
    String contextPath = req.getContextPath();
    String servletPath = req.getServletPath();
    String pathInfo = (req.getPathInfo() == null) ? "" : req.getPathInfo();
    String queryString = (req.getQueryString() == null) ? "" : "?" + req.getQueryString();
    return scheme + serverName + serverPort + contextPath + servletPath + pathInfo + queryString;
  }
}
PrintTasksTitlesServlet.java ফাইল

দ্রষ্টব্য: উপরের বাস্তবায়ন কিছু অ্যাপ ইঞ্জিন লাইব্রেরি ব্যবহার করে, এগুলি সরলীকরণের বিষয় হিসাবে ব্যবহৃত হয়। আপনি যদি অন্য প্ল্যাটফর্মের জন্য বিকাশ করছেন, তাহলে নির্দ্বিধায় ব্যবহারকারীর প্রমাণীকরণ পরিচালনা করে এমন UserService ইন্টারফেস পুনরায় প্রয়োগ করুন।

ব্যবহারকারীর অনুমোদন টোকেন টিকে থাকতে এবং অ্যাক্সেস করতে অ্যাপ্লিকেশনটি একটি DAO ব্যবহার করে। নীচে ইন্টারফেস - OAuthTokenDao - এবং একটি উপহাস (ইন-মেমরি) বাস্তবায়ন - OAuthTokenDaoMemoryImpl - যা এই নমুনায় ব্যবহৃত হয়:

package com.google.oauthsample;

import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;

/**
 * Allows easy storage and access of authorization tokens.
 */
public interface OAuthTokenDao {

  /**
   * Stores the given AccessTokenResponse using the {@code username}, the OAuth
   * {@code clientID} and the tokens scopes as keys.
   *
   * @param tokens The AccessTokenResponse to store
   * @param userName The userName associated wit the token
   */
  public void saveKeys(AccessTokenResponse tokens, String userName);

  /**
   * Returns the AccessTokenResponse stored for the given username, clientId and
   * scopes. Returns {@code null} if there is no AccessTokenResponse for this
   * user and scopes.
   *
   * @param userName The username of which to get the stored AccessTokenResponse
   * @return The AccessTokenResponse of the given username
   */
  public AccessTokenResponse getKeys(String userName);
}
OAuthTokenDao.java ফাইল
package com.google.oauthsample;

import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
...

/**
 * Quick and Dirty memory implementation of {@link OAuthTokenDao} based on
 * HashMaps.
 */
public class OAuthTokenDaoMemoryImpl implements OAuthTokenDao {

  /** Object where all the Tokens will be stored */
  private static Map tokenPersistance = new HashMap();

  public void saveKeys(AccessTokenResponse tokens, String userName) {
    tokenPersistance.put(userName, tokens);
  }

  public AccessTokenResponse getKeys(String userName) {
    return tokenPersistance.get(userName);
  }
}
OAuthTokenDaoMemoryImpl.java ফাইল

এছাড়াও অ্যাপ্লিকেশনটির জন্য OAuth 2.0 শংসাপত্রগুলি একটি বৈশিষ্ট্য ফাইলে সংরক্ষণ করা হয়। বিকল্পভাবে আপনি এগুলিকে আপনার জাভা ক্লাসগুলির একটিতে কোথাও একটি ধ্রুবক হিসাবে রাখতে পারেন, যদিও এখানে OAuthProperties ক্লাস এবং oauth.properties ফাইলটি নমুনায় ব্যবহৃত হচ্ছে:

package com.google.oauthsample;

import ...

/**
 * Object representation of an OAuth properties file.
 */
public class OAuthProperties {

  public static final String DEFAULT_OAUTH_PROPERTIES_FILE_NAME = "oauth.properties";

  /** The OAuth 2.0 Client ID */
  private String clientId;

  /** The OAuth 2.0 Client Secret */
  private String clientSecret;

  /** The Google APIs scopes to access */
  private String scopes;

  /**
   * Instantiates a new OauthProperties object reading its values from the
   * {@code OAUTH_PROPERTIES_FILE_NAME} properties file.
   *
   * @throws IOException IF there is an issue reading the {@code propertiesFile}
   * @throws OauthPropertiesFormatException If the given {@code propertiesFile}
   *           is not of the right format (does not contains the keys {@code
   *           clientId}, {@code clientSecret} and {@code scopes})
   */
  public OAuthProperties() throws IOException {
    this(OAuthProperties.class.getResourceAsStream(DEFAULT_OAUTH_PROPERTIES_FILE_NAME));
  }

  /**
   * Instantiates a new OauthProperties object reading its values from the given
   * properties file.
   *
   * @param propertiesFile the InputStream to read an OAuth Properties file. The
   *          file should contain the keys {@code clientId}, {@code
   *          clientSecret} and {@code scopes}
   * @throws IOException IF there is an issue reading the {@code propertiesFile}
   * @throws OAuthPropertiesFormatException If the given {@code propertiesFile}
   *           is not of the right format (does not contains the keys {@code
   *           clientId}, {@code clientSecret} and {@code scopes})
   */
  public OAuthProperties(InputStream propertiesFile) throws IOException {
    Properties oauthProperties = new Properties();
    oauthProperties.load(propertiesFile);
    clientId = oauthProperties.getProperty("clientId");
    clientSecret = oauthProperties.getProperty("clientSecret");
    scopes = oauthProperties.getProperty("scopes");
    if ((clientId == null) || (clientSecret == null) || (scopes == null)) {
      throw new OAuthPropertiesFormatException();
    }
  }

  /**
   * @return the clientId
   */
  public String getClientId() {
    return clientId;
  }

  /**
   * @return the clientSecret
   */
  public String getClientSecret() {
    return clientSecret;
  }

  /**
   * @return the scopes
   */
  public String getScopesAsString() {
    return scopes;
  }

  /**
   * Thrown when the OAuth properties file was not at the right format, i.e not
   * having the right properties names.
   */
  @SuppressWarnings("serial")
  public class OAuthPropertiesFormatException extends RuntimeException {
  }
}
OAuthProperties.java ফাইল

নীচে oauth.properties ফাইলটি রয়েছে যাতে আপনার আবেদনের OAuth 2.0 শংসাপত্র রয়েছে৷ আপনার নিজের দ্বারা নীচের মান পরিবর্তন করতে হবে।

# Client ID and secret. They can be found in the APIs console.
clientId=1234567890.apps.googleusercontent.com
clientSecret=aBcDeFgHiJkLmNoPqRsTuVwXyZ
# API scopes. Space separated.
scopes=https://www.googleapis.com/auth/tasks
oauth.properties ফাইল

OAuth 2.0 ক্লায়েন্ট আইডি এবং ক্লায়েন্ট সিক্রেট আপনার অ্যাপ্লিকেশন সনাক্ত করে এবং Tasks API-কে আপনার অ্যাপ্লিকেশনের জন্য সংজ্ঞায়িত ফিল্টার এবং কোটা নিয়ম প্রয়োগ করার অনুমতি দেয়। ক্লায়েন্ট আইডি এবং গোপনীয়তা Google APIs কনসোলে পাওয়া যাবে। একবার কনসোলে আপনাকে করতে হবে:

  • একটি প্রকল্প তৈরি করুন বা নির্বাচন করুন।
  • পরিষেবার তালিকায় টাস্ক API স্ট্যাটাস চালু করে টাস্ক এপিআই সক্ষম করুন।
  • API অ্যাক্সেসের অধীনে একটি OAuth 2.0 ক্লায়েন্ট আইডি তৈরি করুন যদি একটি এখনও তৈরি করা না হয়।
  • নিশ্চিত করুন যে প্রকল্পের OAuth 2.0 কোড কলব্যাক হ্যান্ডলার URL পুনঃনির্দেশিত URI- তে নিবন্ধিত/শ্বেত তালিকাভুক্ত। উদাহরণস্বরূপ, এই নমুনা প্রকল্পে আপনাকে https://www.example.com/oauth2callback নিবন্ধন করতে হবে যদি আপনার ওয়েব অ্যাপ্লিকেশনটি https://www.example.com ডোমেন থেকে পরিবেশিত হয়।

APIs কনসোলে URI রিডাইরেক্ট করুন
APIs কনসোলে URI রিডাইরেক্ট করুন

Google অনুমোদনের শেষ পয়েন্ট থেকে অনুমোদন কোডের জন্য শুনুন

যে ক্ষেত্রে ব্যবহারকারী এখনও অ্যাপ্লিকেশনটিকে তার কার্যগুলি অ্যাক্সেস করার জন্য অনুমোদিত করেনি এবং তাই Google এর OAuth 2.0 অনুমোদনের শেষ পয়েন্টে পুনঃনির্দেশিত হয়েছে, ব্যবহারকারীকে Google এর কাছ থেকে একটি অনুমোদন ডায়ালগ প্রদর্শন করা হয় যা ব্যবহারকারীকে তার কার্যগুলিতে আপনার অ্যাপ্লিকেশন অ্যাক্সেস দেওয়ার জন্য বলে:

Google এর অনুমোদন ডায়ালগ
Google এর অনুমোদন ডায়ালগ

অ্যাক্সেস মঞ্জুর বা অস্বীকার করার পরে, ব্যবহারকারীকে OAuth 2.0 কোড কলব্যাক হ্যান্ডলারে পুনঃনির্দেশ করা হবে যা Google অনুমোদন URL তৈরি করার সময় একটি পুনঃনির্দেশ/কলব্যাক হিসাবে নির্দিষ্ট করা হয়েছে:

new GoogleAuthorizationRequestUrl(oauthProperties.getClientId(),
      OAuthCodeCallbackHandlerServlet.getOAuthCodeCallbackHandlerUrl(req), oauthProperties
          .getScopesAsString()).build()

OAuth 2.0 কোড কলব্যাক হ্যান্ডলার - OAuthCodeCallbackHandlerServlet - Google OAuth 2.0 এন্ডপয়েন্ট থেকে পুনঃনির্দেশ পরিচালনা করে। পরিচালনা করার জন্য 2টি কেস রয়েছে:

  • ব্যবহারকারী অ্যাক্সেস মঞ্জুর করেছেন: URL প্যারামিটার থেকে OAuth 2.0 কোড পাওয়ার অনুরোধটি পার্স করে
  • ব্যবহারকারী অ্যাক্সেস অস্বীকার করেছে: ব্যবহারকারীকে একটি বার্তা দেখায়

package com.google.oauthsample;

import ...

/**
 * Servlet handling the OAuth callback from the authentication service. We are
 * retrieving the OAuth code, then exchanging it for a refresh and an access
 * token and saving it.
 */
@SuppressWarnings("serial")
public class OAuthCodeCallbackHandlerServlet extends HttpServlet {

  /** The name of the Oauth code URL parameter */
  public static final String CODE_URL_PARAM_NAME = "code";

  /** The name of the OAuth error URL parameter */
  public static final String ERROR_URL_PARAM_NAME = "error";

  /** The URL suffix of the servlet */
  public static final String URL_MAPPING = "/oauth2callback";

  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // Getting the "error" URL parameter
    String[] error = req.getParameterValues(ERROR_URL_PARAM_NAME);

    // Checking if there was an error such as the user denied access
    if (error != null && error.length > 0) {
      resp.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE, "There was an error: \""+error[0]+"\".");
      return;
    }
    // Getting the "code" URL parameter
    String[] code = req.getParameterValues(CODE_URL_PARAM_NAME);

    // Checking conditions on the "code" URL parameter
    if (code == null || code.length == 0) {
      resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "The \"code\" URL parameter is missing");
      return;
    }
  }

  /**
   * Construct the OAuth code callback handler URL.
   *
   * @param req the HttpRequest object
   * @return The constructed request's URL
   */
  public static String getOAuthCodeCallbackHandlerUrl(HttpServletRequest req) {
    String scheme = req.getScheme() + "://";
    String serverName = req.getServerName();
    String serverPort = (req.getServerPort() == 80) ? "" : ":" + req.getServerPort();
    String contextPath = req.getContextPath();
    String servletPath = URL_MAPPING;
    String pathInfo = (req.getPathInfo() == null) ? "" : req.getPathInfo();
    return scheme + serverName + serverPort + contextPath + servletPath + pathInfo;
  }
}
OAuthCodeCallbackHandlerServlet.java ফাইল

রিফ্রেশ এবং অ্যাক্সেস টোকেনের জন্য অনুমোদন কোড বিনিময় করুন

তারপর, OAuthCodeCallbackHandlerServlet একটি রিফ্রেশ এবং অ্যাক্সেস টোকেনের জন্য Auth 2.0 কোড বিনিময় করে, এটি ডেটাস্টোরে টিকে থাকে এবং ব্যবহারকারীকে PrintTaskListsTitlesServlet URL এ পুনঃনির্দেশ করে:

নীচের ফাইলে যোগ করা কোডটি সিনট্যাক্স হাইলাইট করা হয়েছে, ইতিমধ্যে বিদ্যমান কোডটি ধূসর হয়ে গেছে।

package com.google.oauthsample;

import ...

/**
 * Servlet handling the OAuth callback from the authentication service. We are
 * retrieving the OAuth code, then exchanging it for a refresh and an access
 * token and saving it.
 */
@SuppressWarnings("serial")
public class OAuthCodeCallbackHandlerServlet extends HttpServlet {

  /** The name of the Oauth code URL parameter */
  public static final String CODE_URL_PARAM_NAME = "code";

  /** The name of the OAuth error URL parameter */
  public static final String ERROR_URL_PARAM_NAME = "error";

  /** The URL suffix of the servlet */
  public static final String URL_MAPPING = "/oauth2callback";
/** কলব্যাক পরিচালনা করার পরে ব্যবহারকারীকে রিডাইরেক্ট করার URL। ব্যবহারকারীদের Google * অনুমোদন URL-এ পুনঃনির্দেশিত করার আগে * একটি কুকিতে এটি সংরক্ষণ করার কথা বিবেচনা করুন যদি আপনার কাছে লোকেদের পুনঃনির্দেশিত করার জন্য একাধিক সম্ভাব্য URL থাকে। */ পাবলিক স্ট্যাটিক ফাইনাল স্ট্রিং REDIRECT_URL = "/"; /** OAuth টোকেন DAO বাস্তবায়ন। * একটি স্ট্যাটিক ইনিশিয়ালাইজেশন ব্যবহার করার পরিবর্তে এটিকে ইনজেকশন দেওয়ার কথা বিবেচনা করুন। এছাড়াও আমরা একটি সাধারণ মেমরি বাস্তবায়ন * একটি উপহাস হিসাবে ব্যবহার করছি। আপনার ডাটাবেস সিস্টেম ব্যবহার করে বাস্তবায়ন পরিবর্তন করুন। */ পাবলিক স্ট্যাটিক OAuthTokenDao oauthTokenDao = নতুন OAuthTokenDaoMemoryImpl();
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // Getting the "error" URL parameter
    String[] error = req.getParameterValues(ERROR_URL_PARAM_NAME);

    // Checking if there was an error such as the user denied access
    if (error != null && error.length > 0) {
      resp.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE, "There was an error: \""+error[0]+"\".");
      return;
    }

    // Getting the "code" URL parameter
    String[] code = req.getParameterValues(CODE_URL_PARAM_NAME);

    // Checking conditions on the "code" URL parameter
    if (code == null || code.length == 0) {
      resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "The \"code\" URL parameter is missing");
      return;
    }
// ইনকামিং অনুরোধ URL স্ট্রিং requestUrl = getOAuthCodeCallbackHandlerUrl(req); // OAuth টোকেনের জন্য কোড বিনিময় করুন AccessTokenResponse accessTokenResponse = exchangeCodeForAccessAndRefreshTokens(code[0], requestUrl); // বর্তমান ব্যবহারকারী পাওয়া // এটি অ্যাপ ইঞ্জিনের ব্যবহারকারী পরিষেবা ব্যবহার করছে তবে আপনার এটিকে প্রতিস্থাপন করা উচিত // আপনার নিজস্ব ব্যবহারকারী/লগইন বাস্তবায়ন UserService userService = UserServiceFactory.getUserService(); স্ট্রিং ইমেল = userService.getCurrentUser().getEmail(); // টোকেন সংরক্ষণ করুন oauthTokenDao.saveKeys(accessTokenResponse, email); resp.sendRedirect(REDIRECT_URL); }
  /**
   * Construct the OAuth code callback handler URL.
   *
   * @param req the HttpRequest object
   * @return The constructed request's URL
   */
  public static String getOAuthCodeCallbackHandlerUrl(HttpServletRequest req) {
    String scheme = req.getScheme() + "://";
    String serverName = req.getServerName();
    String serverPort = (req.getServerPort() == 80) ? "" : ":" + req.getServerPort();
    String contextPath = req.getContextPath();
    String servletPath = URL_MAPPING;
    String pathInfo = (req.getPathInfo() == null) ? "" : req.getPathInfo();
    return scheme + serverName + serverPort + contextPath + servletPath + pathInfo;
  }
/** * একটি বিনিময় এবং একটি রিফ্রেশ টোকেনের জন্য প্রদত্ত কোড বিনিময় করে৷ * * @param কোড কোডটি অনুমোদন পরিষেবা থেকে ফিরে পাওয়া গেছে * @param currentUrl কলব্যাকের URL * @param oauthProperties OAuth কনফিগারেশন ধারণকারী বস্তু * @return বস্তুটিতে অ্যাক্সেস এবং রিফ্রেশ টোকেন উভয়ই রয়েছে * @throws IOException */ পাবলিক অ্যাক্সেস টোকেন রেসপন্স এক্সচেঞ্জ কোড ফর অ্যাকসেসএন্ড রিফ্রেশ টোকেনস(স্ট্রিং কোড, স্ট্রিং বর্তমান ইউআরএল) IOException নিক্ষেপ করে { HttpTransport httpTransport = নতুন NetHttpTransport(); জ্যাকসন ফ্যাক্টরি jsonFactory = নতুন জ্যাকসন ফ্যাক্টরি(); // oauth কনফিগারেশন ফাইলটি লোড হচ্ছে OAuthProperties oauthProperties = নতুন OAuthProperties(); নতুন GoogleAuthorizationCodeGrant(httpTransport, jsonFactory, oauthProperties .getClientId(), oauthProperties.getClientSecret(), কোড, currentUrl.execute(); } }
OAuthCodeCallbackHandlerServlet.java ফাইল

দ্রষ্টব্য: উপরের বাস্তবায়ন কিছু অ্যাপ ইঞ্জিন লাইব্রেরি ব্যবহার করে, এগুলি সরলীকরণের বিষয় হিসাবে ব্যবহৃত হয়। আপনি যদি অন্য প্ল্যাটফর্মের জন্য বিকাশ করছেন, তাহলে নির্দ্বিধায় ব্যবহারকারীর প্রমাণীকরণ পরিচালনা করে এমন UserService ইন্টারফেস পুনরায় প্রয়োগ করুন।

ব্যবহারকারীর কাজগুলি পড়ুন এবং তাদের প্রদর্শন করুন

ব্যবহারকারী অ্যাপ্লিকেশনটিকে তার কার্যগুলিতে অ্যাক্সেসের অনুমতি দিয়েছে৷ অ্যাপ্লিকেশনটিতে একটি রিফ্রেশ টোকেন রয়েছে যা OAuthTokenDao-এর মাধ্যমে অ্যাক্সেসযোগ্য ডেটাস্টোরে সংরক্ষণ করা হয়েছে। PrintTaskListsTitlesServlet servlet এখন ব্যবহারকারীর কাজগুলি অ্যাক্সেস করতে এবং সেগুলি প্রদর্শন করতে এই টোকেনগুলি ব্যবহার করতে পারে:

নীচের ফাইলে যোগ করা কোডটি সিনট্যাক্স হাইলাইট করা হয়েছে, ইতিমধ্যে বিদ্যমান কোডটি ধূসর হয়ে গেছে।

package com.google.oauthsample;

import ...

/**
 * Simple sample Servlet which will display the tasks in the default task list of the user.
 */
@SuppressWarnings("serial")
public class PrintTasksTitlesServlet extends HttpServlet {

  /**
   * The OAuth Token DAO implementation, used to persist the OAuth refresh token.
   * Consider injecting it instead of using a static initialization. Also we are
   * using a simple memory implementation as a mock. Change the implementation to
   * using your database system.
   */
  public static OAuthTokenDao oauthTokenDao = new OAuthTokenDaoMemoryImpl();

  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // Getting the current user
    // This is using App Engine's User Service but you should replace this to
    // your own user/login implementation
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();

    // If the user is not logged-in it is redirected to the login service, then back to this page
    if (user == null) {
      resp.sendRedirect(userService.createLoginURL(getFullRequestUrl(req)));
      return;
    }

    // Checking if we already have tokens for this user in store
    AccessTokenResponse accessTokenResponse = oauthTokenDao.getKeys(user.getEmail());

    // If we don't have tokens for this user
    if (accessTokenResponse == null) {
      OAuthProperties oauthProperties = new OAuthProperties();
      // Redirect to the Google OAuth 2.0 authorization endpoint
      resp.sendRedirect(new GoogleAuthorizationRequestUrl(oauthProperties.getClientId(),
          OAuthCodeCallbackHandlerServlet.getOAuthCodeCallbackHandlerUrl(req), oauthProperties
              .getScopesAsString()).build());
      return;
    }
// প্রতিক্রিয়া resp.setContentType("text/plain") এ ব্যবহারকারীর টাস্ক লিস্ট শিরোনাম প্রিন্ট করা; resp.getWriter().append("ব্যবহারকারীর জন্য টাস্ক তালিকা শিরোনাম " + user.getEmail() + ":\n\n"); printTasksTitles(accessTokenResponse, resp.getWriter());
  }

  /**
   * Construct the request's URL without the parameter part.
   *
   * @param req the HttpRequest object
   * @return The constructed request's URL
   */
  public static String getFullRequestUrl(HttpServletRequest req) {
    String scheme = req.getScheme() + "://";
    String serverName = req.getServerName();
    String serverPort = (req.getServerPort() == 80) ? "" : ":" + req.getServerPort();
    String contextPath = req.getContextPath();
    String servletPath = req.getServletPath();
    String pathInfo = (req.getPathInfo() == null) ? "" : req.getPathInfo();
    String queryString = (req.getQueryString() == null) ? "" : "?" + req.getQueryString();
    return scheme + serverName + serverPort + contextPath + servletPath + pathInfo + queryString;
  }
/** * ডিফল্ট * টাস্ক তালিকায় ব্যবহারকারীদের কাজের একটি তালিকা পুনরুদ্ধার করতে Google টাস্ক API ব্যবহার করে। * * @param accessTokenResponse OAuth 2.0 AccessTokenResponse অবজেক্ট * যাতে অ্যাক্সেস টোকেন এবং একটি রিফ্রেশ টোকেন রয়েছে। * @param আউটপুট আউটপুট স্ট্রীম লেখক যেখানে টাস্ক লিস্টের শিরোনাম লিখতে হবে * @return ডিফল্ট টাস্ক লিস্টে ব্যবহারকারীদের টাস্ক শিরোনামের একটি তালিকা। * @throws IOException */ সর্বজনীন অকার্যকর প্রিন্ট টাস্ক টাইটেল(AccessTokenResponse accessTokenResponse, রাইটার আউটপুট) নিক্ষেপ করে IOException { // টাস্ক পরিষেবা শুরু করা HttpTransport transport = new NetHttpTransport(); JsonFactory jsonFactory = new JacksonFactory(); OAuthProperties oauthProperties = নতুন OAuthProperties(); GoogleAccessProtectedResource accessProtectedResource = নতুন GoogleAccessProtectedResource( accessTokenResponse.accessToken, transport, jsonFactory, oauthProperties.getClientId(), oauthProperties.getClientSecret(), accessTokenResponse; টাস্ক সার্ভিস = নতুন টাস্ক (পরিবহন, অ্যাক্সেস প্রোটেক্টেড রিসোর্স, jsonFactory); // কার্য তালিকার তালিকা অনুসন্ধানের জন্য প্রাথমিক কার্য API পরিষেবা ব্যবহার করে com.google.api.services.tasks.model.Tasks tasks = service.tasks.list("@default").execute(); জন্য (টাস্ক টাস্ক : tasks.items) { output.append(task.title + "\n"); } } }
PrintTasksTitlesServlet.java ফাইল

ব্যবহারকারী তার কাজগুলির সাথে প্রদর্শিত হবে:

ব্যবহারকারীর কাজ
ব্যবহারকারীর কাজ

নমুনা আবেদন

এই নমুনা অ্যাপ্লিকেশনের কোডটি এখানে ডাউনলোড করা যেতে পারে। এটা চেক আউট নির্দ্বিধায়.