LinkedIn 集成 - 建立 requestToken

发布于 2024-08-15 12:00:20 字数 2224 浏览 8 评论 0原文

我正在开发(目前正在尝试)将与 LinkedIn 集成的 portlet。

按照有关它的文档: http://developer.linkedin.com/docs/DOC-1008 --> ; 授权 LinkedIn 会员的第一步是请求 requestToken。该请求通过 HTTP POST 完成。 对于 requestToken 步骤,您的签名字符串中应包含以下组件:

* HTTP Method (POST)
* Request URI (https://api.linkedin.com/uas/oauth/requestToken)
* oauth_callback
* oauth_consumer_key
* oauth_nonce
* oauth_signature_method
* oauth_timestamp
* oauth_version

我已经有 API(它是 oauth_consumer_key)密钥,并且我需要生成特定的 URL 字符串。 有这个 URL 和 HTTP 连接的下一个 java 代码:

private void processAuthentication() {

    Calendar cal = Calendar.getInstance();
    Long ms = cal.getTimeInMillis();
    Long timestamp = ms / 1000;

    Random r = new Random();
    Long nonce = r.nextLong();

    String prefixUrl = "https://api.linkedin.com/uas/oauth/requestToken";
    String oauthCallback = "oauth_callback=http://localhost/";
    String oauthConsumerKey =
            "&oauth_consumer_key=my_consumer_key";
    String oauthNonce = "&oauth_nonce=" + nonce.toString();
    String oauthSignatureMethod = "&oauth_signature_method=HMAC-SHA1";
    String oauthTimestamp = "&oauth_timestamp=" + timestamp.toString();
    String oauthVersion = "&oauth_version=1.0";

    String mainUrl =
            oauthCallback + oauthConsumerKey + oauthNonce + oauthSignatureMethod
                    + oauthTimestamp + oauthVersion;

    try {
        prefixUrl =
                URLEncoder.encode(prefixUrl, "UTF-8") + "&"
                        + URLEncoder.encode(mainUrl, "UTF-8");

        URL url = new URL(prefixUrl);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        String msg = connection.getResponseMessage();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

对于那些遇到过这个问题的人来说,下一个问题是: 应该如何真正查看连接的 URL 字符串以及如何接收响应?

对于 URL,它对您生成的 URL 示例感兴趣。 对于感兴趣的响应,获取它的方法。 据我了解,建立 HTTP 连接后,响应是:

connection.getResponseMessage();

I'm developing (trying for now) portlet that will be integrated with LinkedIn.

Following the documentation about it:
http://developer.linkedin.com/docs/DOC-1008 -->
The first step to authorizing a LinkedIn member is requesting a requestToken. This request is done with an HTTP POST.
For the requestToken step, the following components should be present in your string to sign:

* HTTP Method (POST)
* Request URI (https://api.linkedin.com/uas/oauth/requestToken)
* oauth_callback
* oauth_consumer_key
* oauth_nonce
* oauth_signature_method
* oauth_timestamp
* oauth_version

I have already API(it's oauth_consumer_key) key and i need to generate specific URL string.
Have next java code for this URL and HTTP connection:

private void processAuthentication() {

    Calendar cal = Calendar.getInstance();
    Long ms = cal.getTimeInMillis();
    Long timestamp = ms / 1000;

    Random r = new Random();
    Long nonce = r.nextLong();

    String prefixUrl = "https://api.linkedin.com/uas/oauth/requestToken";
    String oauthCallback = "oauth_callback=http://localhost/";
    String oauthConsumerKey =
            "&oauth_consumer_key=my_consumer_key";
    String oauthNonce = "&oauth_nonce=" + nonce.toString();
    String oauthSignatureMethod = "&oauth_signature_method=HMAC-SHA1";
    String oauthTimestamp = "&oauth_timestamp=" + timestamp.toString();
    String oauthVersion = "&oauth_version=1.0";

    String mainUrl =
            oauthCallback + oauthConsumerKey + oauthNonce + oauthSignatureMethod
                    + oauthTimestamp + oauthVersion;

    try {
        prefixUrl =
                URLEncoder.encode(prefixUrl, "UTF-8") + "&"
                        + URLEncoder.encode(mainUrl, "UTF-8");

        URL url = new URL(prefixUrl);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        String msg = connection.getResponseMessage();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

The question is next,for those, who had faced this problem:
How should really look URL string for connection and how response is received?

For URL, it's interested the example of URL, you generated.
And for response interested, method to get it.
As i understand, after HTTP connection been established,that response is:

connection.getResponseMessage();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魂归处 2024-08-22 12:00:20

@sergionni 我从 linkedin-developer 找到了你问题的答案

如你所知
授权 Linked-In 成员的第一步是请求 requestToken。该请求通过 HTTP POST 完成。

如果您使用回调,您的基本字符串最终应该看起来像这样:

POST&https%3A%2F%2Fapi.linkedin.com%2Fuas%2Foauth%2FrequestToken
&oauth_callback%3Dhttp%253A%252F%252Flocalhost%252Foauth_callback%26o
auth_consumer_key%3DABCDEFGHIJKLMNOPQRSTUVWXYZ%26
oauth_nonce%3DoqwgSYFUD87MHmJJDv7bQqOF2EPnVus7Wkqj5duNByU%26
oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1259178158%26
oauth_version%3D1.0

然后,您使用 Consumer_secret 签署此基本字符串,计算签名。在这种情况下,如果您的密钥是 1234567890,则签名将为 TLQXuUzM7omwDbtXimn6bLDvfF8=。

现在,您可以使用生成的签名以及 oauth_nonce、oauth_callback、oauth_signature_method、oauth_timestamp、oauth_consumer_key 和 oauth_version 并创建 HTTP 授权标头。对于此请求,该 HTTP 标头将如下所示:

授权:OAuth
oauth_nonce="oqwgSYFUD87MHmJJDv7bQqOF2EPnVus7Wkqj5duNByU",
oauth_callback =“http%3A%2F%2Flocalhost%2Foauth_callback”,
oauth_signature_method="HMAC-SHA1",
oauth_timestamp =“1259178158”,
oauth_consumer_key="ABCDEFGHIJKLMNOPQRSTUVWXYZ",
oauth_signature="TLQXuUzM7omwDbtXimn6bLDvfF8=",
oauth_version="1.0"

请注意,HTTP 标头是单个标头——而不是每个组件的 HTTP 标头。您可以选择提供领域=“http://api.linkedin.com”。

作为对 requestToken 请求的响应,您的 requestToken 将位于“oauth_token”响应字段中,这是我们使用“oauth_callback_confirmed”字段、oauth_token_secret 和 oauth_expires_in 以及一些其他值确认您的回调的验证。

(这里是您的答案)回复如下:

oauth_token=94ab03c4-ae2c-45e4-8732-0e6c4899db63
&oauth_token_secret=be6ccb24-bf0a-4ea8-a4b1-0a70508e452b
&oauth_callback_confirmed=true&oauth_expires_in=599

@sergionni I found answer to your Question from linkedin-developer

As you know
The first step to authorizing a Linked-In member is requesting a requestToken. This request is done with an HTTP POST.

Your base string should end up looking something like this if you're using a callback:

POST&https%3A%2F%2Fapi.linkedin.com%2Fuas%2Foauth%2FrequestToken
&oauth_callback%3Dhttp%253A%252F%252Flocalhost%252Foauth_callback%26o
auth_consumer_key%3DABCDEFGHIJKLMNOPQRSTUVWXYZ%26
oauth_nonce%3DoqwgSYFUD87MHmJJDv7bQqOF2EPnVus7Wkqj5duNByU%26
oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1259178158%26
oauth_version%3D1.0

You then sign this base string with your consumer_secret, computing a signature. In this case, if your secret was 1234567890, the signature would be TLQXuUzM7omwDbtXimn6bLDvfF8=.

Now you take the signature you generated, along with oauth_nonce, oauth_callback, oauth_signature_method, oauth_timestamp, oauth_consumer_key, and oauth_version and create an HTTP Authorization header. For this request, that HTTP header would look like:

Authorization: OAuth
oauth_nonce="oqwgSYFUD87MHmJJDv7bQqOF2EPnVus7Wkqj5duNByU",
oauth_callback="http%3A%2F%2Flocalhost%2Foauth_callback",
oauth_signature_method="HMAC-SHA1",
oauth_timestamp="1259178158",
oauth_consumer_key="ABCDEFGHIJKLMNOPQRSTUVWXYZ",
oauth_signature="TLQXuUzM7omwDbtXimn6bLDvfF8=",
oauth_version="1.0"

Please note, that the HTTP header is a single header -- not an HTTP header for each component. You can optionally supply a realm="http://api.linkedin.com".

As a response to your request for a requestToken, your requestToken will be in the "oauth_token" response field, a validation that we acknowledged your callback with the "oauth_callback_confirmed" field, an oauth_token_secret, and a oauth_expires_in, and a few other values.

(here us Your answaer) response would look like:

oauth_token=94ab03c4-ae2c-45e4-8732-0e6c4899db63
&oauth_token_secret=be6ccb24-bf0a-4ea8-a4b1-0a70508e452b
&oauth_callback_confirmed=true&oauth_expires_in=599

慕烟庭风 2024-08-22 12:00:20

您可以尝试使用 OAuth 库来处理连接:http://code.google.com/p /oauth/

You might try out the OAuth libraries to handle the connection: http://code.google.com/p/oauth/

海螺姑娘 2024-08-22 12:00:20

我为 Play Framework 创建了一个插件,可以轻松地与 LinkedIn 的 OAuth 集成:geeks.aretotally.in/projects/play-framework-linkedin-module。希望它能有所帮助。你应该看看 Play,非常非常酷的 Java 框架。

I created a plugin for Play Framework to easily integrated with LinkedIn's OAuth: geeks.aretotally.in/projects/play-framework-linkedin-module. Hopefully it can help. You should def check out Play, very very cool Java framework.

傻比既视感 2024-08-22 12:00:20

门户组件主体:

public class LinkedInPortlet extends GenericPortlet {

    public static final String PAGE_PIN = "pin";

    public static final String PAGE_EDIT = "edit";

    public static final String PAGE_PROFILE = "profile";

    public static final String PAGE_CONNECTIONS = "connections";

    public static final String FORM_LINKEDIN_PREFERENCES = "preferencesLinkedInForm";

    public static final String PAGE_VIEW_MY_PROFILE = "/WEB-INF/portlets/linkedin/myProfile.jsp";

    public static final String PAGE_VIEW_MY_CONNECTIONS =
            "/WEB-INF/portlets/linkedin/myConnections.jsp";

    public static final String PAGE_PREFERENCES = "/WEB-INF/portlets/linkedin/edit.jsp";

    public void doView(RenderRequest request, RenderResponse response) throws PortletException,
        IOException {

        String view = PAGE_VIEW_MY_PROFILE;

        String page =
                (String) request.getPortletSession().getAttribute(
                        "page_" + getPortletIdentifier(request), PortletSession.PORTLET_SCOPE);

        String accessTokenToken =
                getStringConfiguration(request, LinkedInPreferencesForm.PARAM_ACCESS_TOKEN_TOKEN);
        String accessTokenSecret =
                getStringConfiguration(request, LinkedInPreferencesForm.PARAM_ACCESS_TOKEN_SECRET);

        LinkedInContact profile = new LinkedInContact();
        List<LinkedInContact> contacts = new ArrayList<LinkedInContact>();

        if (PAGE_PIN.equals(page)) {
            view = PAGE_PREFERENCES;
        } else if (PAGE_EDIT.equals(page)) {
            view = PAGE_PREFERENCES;
        } else if (PAGE_CONNECTIONS.equals(page)) {
            try {
                contacts =
                        ServiceResolver.getResolver().getLinkedInService().getConnections(
                                accessTokenToken, accessTokenSecret);
            } catch (ServiceException se) {
                view = PAGE_PREFERENCES;
                handleException(request, se);
            }
            view = PAGE_VIEW_MY_CONNECTIONS;
        } else {
            try {
                profile =
                        ServiceResolver.getResolver().getLinkedInService().getProfile(
                                accessTokenToken, accessTokenSecret);
            } catch (ServiceException se) {
                view = PAGE_PREFERENCES;
                handleException(request, se);
            }

            view = PAGE_VIEW_MY_PROFILE;
        }

        request.setAttribute("profile", profile);
        request.setAttribute("contacts", contacts);

        response.setContentType(request.getResponseContentType());
        PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(view);
        rd.include(request, response);

    }

    public void processAction(ActionRequest request, ActionResponse response)
        throws PortletException, IOException {

        String action;

        action = (String) request.getParameter("action");

        String page = request.getParameter("page");

        if (page == null) {
            page = PAGE_PROFILE;
        } else if ("auth".equals(action)) {
            request.getPortletSession().setAttribute(
                    "requestToken_" + getPortletIdentifier(request),
                    ServiceResolver.getResolver().getLinkedInService().getRequestToken(),
                    PortletSession.APPLICATION_SCOPE);
            LinkedInPreferencesForm form = new LinkedInPreferencesForm(request);
            request.getPortletSession().setAttribute(
                    FORM_LINKEDIN_PREFERENCES + getPortletIdentifier(request), form,
                    PortletSession.APPLICATION_SCOPE);
            response.setPortletMode(PortletMode.EDIT);
        } else if ("save".equals(action)) {
            try {
                try {
                    savePreferences(request, response);
                } catch (ServiceException e) {
                    handleException(request, e);
                }
            } catch (PortletModeException e) {
                handleException(request, e);
            }
        } else if ("myProfile".equals(action)) {
            page = PAGE_PROFILE;
        } else if ("myConnections".equals(action)) {
            page = PAGE_CONNECTIONS;
        }

        if (page != null) {
            request.getPortletSession().setAttribute("page_" + getPortletIdentifier(request), page,
                    PortletSession.PORTLET_SCOPE);
        }
    }

    private void savePreferences(ActionRequest request, ActionResponse response)
        throws PortletModeException, ServiceException {

        LinkedInPreferencesForm form = new LinkedInPreferencesForm(request);

        if (validateForm(request, form)) {

            LinkedInRequestToken requestToken =
                    (LinkedInRequestToken) request.getPortletSession().getAttribute(
                            "requestToken_" + getPortletIdentifier(request),
                            PortletSession.APPLICATION_SCOPE);

            String pin = request.getParameter("pinCode");

            LinkedInAccessToken accessToken;

            try {
                accessToken =
                        ServiceResolver.getResolver().getLinkedInService().getAccessToken(
                                requestToken, pin);
            } catch (LinkedInOAuthServiceException ase) {
                response.setPortletMode(PortletMode.EDIT);
                throw new ServiceException("linkedin.authentication.failed");

            }

            String tokenToken = requestToken.getToken();
            String secret = requestToken.getTokenSecret();
            String tokenURL = requestToken.getAuthorizationUrl();

            Properties configuration = new Properties();
            configuration.setProperty(LinkedInPreferencesForm.PARAM_PIN, form.getPin());
            configuration
                    .setProperty(LinkedInPreferencesForm.PARAM_REQUEST_TOKEN_TOKEN, tokenToken);
            configuration.setProperty(LinkedInPreferencesForm.PARAM_REQUEST_TOKEN_SECRET, secret);
            configuration.setProperty(LinkedInPreferencesForm.PARAM_REQUEST_TOKEN_URL, tokenURL);
            configuration.setProperty(LinkedInPreferencesForm.PARAM_ACCESS_TOKEN_TOKEN, accessToken
                    .getToken());

            configuration.setProperty(LinkedInPreferencesForm.PARAM_ACCESS_TOKEN_SECRET,
                    accessToken.getTokenSecret());
            ServiceResolver.getResolver().getPortalService().savePortletConfiguration(request,
                    configuration);

            resetSessionForm(request, FORM_LINKEDIN_PREFERENCES);
            response.setPortletMode(PortletMode.VIEW);

        } else {
            // store in session
            request.getPortletSession().setAttribute(
                    FORM_LINKEDIN_PREFERENCES + getPortletIdentifier(request), form,
                    PortletSession.APPLICATION_SCOPE);
            response.setPortletMode(PortletMode.EDIT);
            logger.debug(FORM_LINKEDIN_PREFERENCES + " is in edit mode");
        }
    }

    @Override
    protected void addConfiguration(MessageSource messageSource, Locale locale,
        Map<String, String> result) {
        result.put(LinkedInPreferencesForm.PARAM_PIN, messageSource.getMessage(
                "linkedIn.preferences.pin", null, locale));
        result.put(LinkedInPreferencesForm.PARAM_REQUEST_TOKEN_TOKEN, messageSource.getMessage(
                "linkedIn.preferences.requestTokenToken", null, locale));
        result.put(LinkedInPreferencesForm.PARAM_REQUEST_TOKEN_SECRET, messageSource.getMessage(
                "linkedIn.preferences.requestTokenSecret", null, locale));
        result.put(LinkedInPreferencesForm.PARAM_REQUEST_TOKEN_URL, messageSource.getMessage(
                "linkedIn.preferences.requestTokenURL", null, locale));
        result.put(LinkedInPreferencesForm.PARAM_ACCESS_TOKEN_TOKEN, messageSource.getMessage(
                "linkedIn.preferences.accessToken", null, locale));
        result.put(LinkedInPreferencesForm.PARAM_ACCESS_TOKEN_SECRET, messageSource.getMessage(
                "linkedIn.preferences.accessTokenSecret", null, locale));
    }

    @Override
    protected void addPreference(MessageSource messageSource, Locale locale,
        Map<String, String> result) {
    }

    @Override
    public String getAsyncTitle(RenderRequest request) {
        return this.getTitle(request);
    }

    protected boolean validateForm(ActionRequest request, LinkedInPreferencesForm form) {
        return form.validate();
    }

    protected String myEdit(RenderRequest request, RenderResponse response)
        throws PortletException, IOException {

        LinkedInPreferencesForm form = new LinkedInPreferencesForm();

        form.setPin(getStringConfiguration(request, LinkedInPreferencesForm.PARAM_PIN));
        form.setRequestTokenToken(getStringConfiguration(request,
                LinkedInPreferencesForm.PARAM_REQUEST_TOKEN_TOKEN));
        form.setRequestTokenSecret(getStringConfiguration(request,
                LinkedInPreferencesForm.PARAM_REQUEST_TOKEN_SECRET));
        form.setRequestTokenURL(getStringConfiguration(request,
                LinkedInPreferencesForm.PARAM_REQUEST_TOKEN_URL));

        registerSessionForm(request, FORM_LINKEDIN_PREFERENCES, form);

        LinkedInRequestToken requestToken;

        requestToken =
                (LinkedInRequestToken) request.getPortletSession().getAttribute(
                        "requestToken_" + getPortletIdentifier(request),
                        PortletSession.APPLICATION_SCOPE);

        if (requestToken == null) {
            requestToken =
                    new LinkedInRequestToken(form.getRequestTokenToken(), form
                            .getRequestTokenSecret());
            requestToken.setAuthorizationUrl(form.getRequestTokenURL());
        }

        request.setAttribute("requestToken", requestToken);

        return PAGE_PREFERENCES;

    }
}

portlet body:

public class LinkedInPortlet extends GenericPortlet {

    public static final String PAGE_PIN = "pin";

    public static final String PAGE_EDIT = "edit";

    public static final String PAGE_PROFILE = "profile";

    public static final String PAGE_CONNECTIONS = "connections";

    public static final String FORM_LINKEDIN_PREFERENCES = "preferencesLinkedInForm";

    public static final String PAGE_VIEW_MY_PROFILE = "/WEB-INF/portlets/linkedin/myProfile.jsp";

    public static final String PAGE_VIEW_MY_CONNECTIONS =
            "/WEB-INF/portlets/linkedin/myConnections.jsp";

    public static final String PAGE_PREFERENCES = "/WEB-INF/portlets/linkedin/edit.jsp";

    public void doView(RenderRequest request, RenderResponse response) throws PortletException,
        IOException {

        String view = PAGE_VIEW_MY_PROFILE;

        String page =
                (String) request.getPortletSession().getAttribute(
                        "page_" + getPortletIdentifier(request), PortletSession.PORTLET_SCOPE);

        String accessTokenToken =
                getStringConfiguration(request, LinkedInPreferencesForm.PARAM_ACCESS_TOKEN_TOKEN);
        String accessTokenSecret =
                getStringConfiguration(request, LinkedInPreferencesForm.PARAM_ACCESS_TOKEN_SECRET);

        LinkedInContact profile = new LinkedInContact();
        List<LinkedInContact> contacts = new ArrayList<LinkedInContact>();

        if (PAGE_PIN.equals(page)) {
            view = PAGE_PREFERENCES;
        } else if (PAGE_EDIT.equals(page)) {
            view = PAGE_PREFERENCES;
        } else if (PAGE_CONNECTIONS.equals(page)) {
            try {
                contacts =
                        ServiceResolver.getResolver().getLinkedInService().getConnections(
                                accessTokenToken, accessTokenSecret);
            } catch (ServiceException se) {
                view = PAGE_PREFERENCES;
                handleException(request, se);
            }
            view = PAGE_VIEW_MY_CONNECTIONS;
        } else {
            try {
                profile =
                        ServiceResolver.getResolver().getLinkedInService().getProfile(
                                accessTokenToken, accessTokenSecret);
            } catch (ServiceException se) {
                view = PAGE_PREFERENCES;
                handleException(request, se);
            }

            view = PAGE_VIEW_MY_PROFILE;
        }

        request.setAttribute("profile", profile);
        request.setAttribute("contacts", contacts);

        response.setContentType(request.getResponseContentType());
        PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(view);
        rd.include(request, response);

    }

    public void processAction(ActionRequest request, ActionResponse response)
        throws PortletException, IOException {

        String action;

        action = (String) request.getParameter("action");

        String page = request.getParameter("page");

        if (page == null) {
            page = PAGE_PROFILE;
        } else if ("auth".equals(action)) {
            request.getPortletSession().setAttribute(
                    "requestToken_" + getPortletIdentifier(request),
                    ServiceResolver.getResolver().getLinkedInService().getRequestToken(),
                    PortletSession.APPLICATION_SCOPE);
            LinkedInPreferencesForm form = new LinkedInPreferencesForm(request);
            request.getPortletSession().setAttribute(
                    FORM_LINKEDIN_PREFERENCES + getPortletIdentifier(request), form,
                    PortletSession.APPLICATION_SCOPE);
            response.setPortletMode(PortletMode.EDIT);
        } else if ("save".equals(action)) {
            try {
                try {
                    savePreferences(request, response);
                } catch (ServiceException e) {
                    handleException(request, e);
                }
            } catch (PortletModeException e) {
                handleException(request, e);
            }
        } else if ("myProfile".equals(action)) {
            page = PAGE_PROFILE;
        } else if ("myConnections".equals(action)) {
            page = PAGE_CONNECTIONS;
        }

        if (page != null) {
            request.getPortletSession().setAttribute("page_" + getPortletIdentifier(request), page,
                    PortletSession.PORTLET_SCOPE);
        }
    }

    private void savePreferences(ActionRequest request, ActionResponse response)
        throws PortletModeException, ServiceException {

        LinkedInPreferencesForm form = new LinkedInPreferencesForm(request);

        if (validateForm(request, form)) {

            LinkedInRequestToken requestToken =
                    (LinkedInRequestToken) request.getPortletSession().getAttribute(
                            "requestToken_" + getPortletIdentifier(request),
                            PortletSession.APPLICATION_SCOPE);

            String pin = request.getParameter("pinCode");

            LinkedInAccessToken accessToken;

            try {
                accessToken =
                        ServiceResolver.getResolver().getLinkedInService().getAccessToken(
                                requestToken, pin);
            } catch (LinkedInOAuthServiceException ase) {
                response.setPortletMode(PortletMode.EDIT);
                throw new ServiceException("linkedin.authentication.failed");

            }

            String tokenToken = requestToken.getToken();
            String secret = requestToken.getTokenSecret();
            String tokenURL = requestToken.getAuthorizationUrl();

            Properties configuration = new Properties();
            configuration.setProperty(LinkedInPreferencesForm.PARAM_PIN, form.getPin());
            configuration
                    .setProperty(LinkedInPreferencesForm.PARAM_REQUEST_TOKEN_TOKEN, tokenToken);
            configuration.setProperty(LinkedInPreferencesForm.PARAM_REQUEST_TOKEN_SECRET, secret);
            configuration.setProperty(LinkedInPreferencesForm.PARAM_REQUEST_TOKEN_URL, tokenURL);
            configuration.setProperty(LinkedInPreferencesForm.PARAM_ACCESS_TOKEN_TOKEN, accessToken
                    .getToken());

            configuration.setProperty(LinkedInPreferencesForm.PARAM_ACCESS_TOKEN_SECRET,
                    accessToken.getTokenSecret());
            ServiceResolver.getResolver().getPortalService().savePortletConfiguration(request,
                    configuration);

            resetSessionForm(request, FORM_LINKEDIN_PREFERENCES);
            response.setPortletMode(PortletMode.VIEW);

        } else {
            // store in session
            request.getPortletSession().setAttribute(
                    FORM_LINKEDIN_PREFERENCES + getPortletIdentifier(request), form,
                    PortletSession.APPLICATION_SCOPE);
            response.setPortletMode(PortletMode.EDIT);
            logger.debug(FORM_LINKEDIN_PREFERENCES + " is in edit mode");
        }
    }

    @Override
    protected void addConfiguration(MessageSource messageSource, Locale locale,
        Map<String, String> result) {
        result.put(LinkedInPreferencesForm.PARAM_PIN, messageSource.getMessage(
                "linkedIn.preferences.pin", null, locale));
        result.put(LinkedInPreferencesForm.PARAM_REQUEST_TOKEN_TOKEN, messageSource.getMessage(
                "linkedIn.preferences.requestTokenToken", null, locale));
        result.put(LinkedInPreferencesForm.PARAM_REQUEST_TOKEN_SECRET, messageSource.getMessage(
                "linkedIn.preferences.requestTokenSecret", null, locale));
        result.put(LinkedInPreferencesForm.PARAM_REQUEST_TOKEN_URL, messageSource.getMessage(
                "linkedIn.preferences.requestTokenURL", null, locale));
        result.put(LinkedInPreferencesForm.PARAM_ACCESS_TOKEN_TOKEN, messageSource.getMessage(
                "linkedIn.preferences.accessToken", null, locale));
        result.put(LinkedInPreferencesForm.PARAM_ACCESS_TOKEN_SECRET, messageSource.getMessage(
                "linkedIn.preferences.accessTokenSecret", null, locale));
    }

    @Override
    protected void addPreference(MessageSource messageSource, Locale locale,
        Map<String, String> result) {
    }

    @Override
    public String getAsyncTitle(RenderRequest request) {
        return this.getTitle(request);
    }

    protected boolean validateForm(ActionRequest request, LinkedInPreferencesForm form) {
        return form.validate();
    }

    protected String myEdit(RenderRequest request, RenderResponse response)
        throws PortletException, IOException {

        LinkedInPreferencesForm form = new LinkedInPreferencesForm();

        form.setPin(getStringConfiguration(request, LinkedInPreferencesForm.PARAM_PIN));
        form.setRequestTokenToken(getStringConfiguration(request,
                LinkedInPreferencesForm.PARAM_REQUEST_TOKEN_TOKEN));
        form.setRequestTokenSecret(getStringConfiguration(request,
                LinkedInPreferencesForm.PARAM_REQUEST_TOKEN_SECRET));
        form.setRequestTokenURL(getStringConfiguration(request,
                LinkedInPreferencesForm.PARAM_REQUEST_TOKEN_URL));

        registerSessionForm(request, FORM_LINKEDIN_PREFERENCES, form);

        LinkedInRequestToken requestToken;

        requestToken =
                (LinkedInRequestToken) request.getPortletSession().getAttribute(
                        "requestToken_" + getPortletIdentifier(request),
                        PortletSession.APPLICATION_SCOPE);

        if (requestToken == null) {
            requestToken =
                    new LinkedInRequestToken(form.getRequestTokenToken(), form
                            .getRequestTokenSecret());
            requestToken.setAuthorizationUrl(form.getRequestTokenURL());
        }

        request.setAttribute("requestToken", requestToken);

        return PAGE_PREFERENCES;

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文