使用JOAuth库在LinkedIn中发布职位 [英] Using JOAuth library to post jobs in LinkedIn

查看:76
本文介绍了使用JOAuth库在LinkedIn中发布职位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在 LinkedIn Job Posting API实现Java中发布的问题的延续.我是发布作业的基于OAuth的身份验证的新手,并且在此过程中也正在学习.如果我的问题很简单/天真,请与我联系.

This is in continuation of a question posted by me on LinkedIn Job Posting API implementation in Java. I'm new to this OAuth based authentication of posting Jobs and am also learning as well during this process. Please bear with me if my questions are very basic/naive.

我正尝试使用 JOAuth 库进行OAuth身份验证和发布职位到领英.我正在使用OAuth2调用.我对 JOAuth 库有以下疑问:

Am trying to use JOAuth library for OAuth authentication and post jobs to LinkedIn. I'm using OAuth2 calls. I've the following questions with JOAuth library:

  1. 在JOAuth链接所示的示例中,如何获取LinkedIn的请求令牌?我找不到任何请求请求令牌的声明.但是我可以看到 consumer.generateRequestAuthorizationUrl(ResponseType.CODE,redirectUri,null,(String [])null));
  2. 如果我想使用基于 oob 的回调重定向,那么我需要在 redirectUri 中传递/设置什么?
  3. 如果一切成功,并且我拥有访问令牌,那么最终如何在
  1. In the example shown at the JOAuth link, how do I get Request Token for LinkedIn? I don't find any statement for requesting Request Token. But I could see consumer.generateRequestAuthorizationUrl(ResponseType.CODE, redirectUri, null, (String[])null));
  2. If I want to use oob based callback redirection, then what do I need to pass/set in redirectUri?
  3. If everything is successful and if I've the Access Token, how do I finally submit/send my Job data XML at http://api.linkedin.com/v1/jobs?

推荐答案

您很困惑.LinkedIn 使用 OAuth 1 协议而不是 OAuth 2 协议.这是您对LinkedIn进行Oauth 1授权的方法.

You're confused. LinkedIn uses OAuth 1 protocol and not OAuth 2 protocols. Here's how you would do Oauth 1 authorization to/with LinkedIn.

如果要创建Web应用程序,请在 WEB-INF 文件夹下,创建 oauth-config.xml 文件,并具有与以下类似的配置:

If you're creating a Web application, under WEB-INF folder, create a oauth-config.xml file and have a configuration similar to this:

<?xml version="1.0" encoding="UTF-8"?>
<oauth-config>
    <!-- LinkedIn OAuth Config -->
        <oauth name="linkedIn" version="1">
                <consumer key="API_KEY" secret="API_SECRET" />
                <provider requestTokenUrl="https://api.linkedin.com/uas/oauth/requestToken" authorizationUrl="https://api.linkedin.com/uas/oauth/authorize" accessTokenUrl="https://api.linkedin.com/uas/oauth/accessToken" />
        </oauth>

        <service path="/authorize_ready" class="com.neurologic.example.LinkedInOAuthService" oauth="linkedIn">
                <success path="/start.htm" />
        </service>
</oauth-config>

LinkedIn使用OAuth版本1(因此为该版本).

LinkedIn uses OAuth version 1 (hence the version).

在您的 WEB-INF \ web.xml 下,添加以下内容:

Under your WEB-INF\web.xml, add the following:

<servlet>
    <description>An OAuth Servlet Controller</description>
    <display-name>OAuthServlet</display-name>
    <servlet-name>OAuthServlet</servlet-name>
    <servlet-class>com.neurologic.oauth.servlet.OAuthServlet</servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/oauth-config.xml</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>OAuthServlet</servlet-name>
    <url-pattern>/oauth/*</url-pattern>
</servlet-mapping>

现在,我们需要创建一个服务,该服务将从Linked In接收授权令牌.

Now, we need to create a service that will receive the authorized token from Linked In.

package com.neurologic.example;

import javax.servlet.http.HttpServletRequest;

import net.oauth.signature.OAuthSignature;
import net.oauth.signature.impl.OAuthHmacSha1Signature;
import net.oauth.token.v1.AccessToken;
import net.oauth.token.v1.RequestToken;

import com.neurologic.oauth.service.impl.OAuth1Service;

/**
 * @author Buhake Sindi
 * @since 31 May 2011
 *
 */
public class LinkedInOAuthService extends OAuth1Service {

    public static final String LINKED_IN_REQUEST_TOKEN_SESSION = "LINKED_IN_REQUEST_TOKEN_SESSION";
    public static final String LINKED_IN_ACCESS_TOKEN_SESSION = "LINKED_IN_ACCESS_TOKEN_SESSION";

    /* (non-Javadoc)
     * @see com.neurologic.oauth.service.impl.OAuth1Service#getOAuthSignature()
     */
    @Override
    protected OAuthSignature getOAuthSignature() {
        // TODO Auto-generated method stub
        return new OAuthHmacSha1Signature();
    }

    /* (non-Javadoc)
     * @see com.neurologic.oauth.service.impl.OAuth1Service#getRealm()
     */
    @Override
    protected String getRealm() {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.neurologic.oauth.service.impl.OAuth1Service#getRequestToken(javax.servlet.http.HttpServletRequest)
     */
    @Override
    protected RequestToken getRequestToken(HttpServletRequest request) {
        // TODO Auto-generated method stub
        return (RequestToken) request.getSession().getAttribute(LINKED_IN_REQUEST_TOKEN_SESSION);
    }

    /* (non-Javadoc)
     * @see com.neurologic.oauth.service.OAuthService#saveAccessToken(javax.servlet.http.HttpServletRequest, java.lang.Object)
     */
    @Override
    public void saveAccessToken(HttpServletRequest request, AccessToken accessToken) {
        // TODO Auto-generated method stub
        request.getSession().setAttribute(LINKED_IN_ACCESS_TOKEN_SESSION, accessToken);
    }
}

现在,使用以下示例:

package com.neurologic.example;

import net.oauth.consumer.OAuth1Consumer;
import net.oauth.exception.OAuthException;
import net.oauth.provider.OAuth1ServiceProvider;
import net.oauth.signature.impl.OAuthHmacSha1Signature;
import net.oauth.token.v1.AccessToken;
import net.oauth.token.v1.AuthorizedToken;
import net.oauth.token.v1.RequestToken;

/**
 * @author Buhake Sindi
 * @since 14 June 2011
 *
 */
public class LinkedInExample {

    private static final String LINKEDIN_API_URL = "https://api.linkedin.com";
    private static final String API_KEY = "";
    private static final String API_SECRET  = "";
    private static final String CALLBACK_URL = "http://localhost:8080/myapp/oauth/authorize_ready";
    private OAuth1Consumer consumer;


    /**
     * 
     */
    public LinkedInExample() {
        super();
        // TODO Auto-generated constructor stub
        consumer = new OAuth1Consumer(API_KEY, API_SECRET, new OAuth1ServiceProvider(LINKEDIN_API_URL + "/uas/oauth/requestToken", LINKEDIN_API_URL + "/uas/oauth/authorize", LINKEDIN_API_URL + "/uas/oauth/accessToken"));
    }

    public RequestToken requestUnauthorizedRequestToken() throws OAuthException {
        return consumer.requestUnauthorizedToken(null, CALLBACK_URL, null, new OAuthHmacSha1Signature());
    }

    public String getAuthorizationUrl(RequestToken token) throws OAuthException {
        return consumer.createOAuthUserAuthorizationUrl(token, null);
    }

    public AccessToken requestAccessToken(AuthorizedToken authorizedToken, RequestToken token) throws OAuthException {
        return consumer.requestAccessToken(null, authorizedToken, token.getTokenSecret(), new OAuthHmacSha1Signature());
    }


    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            LinkedInExample example = new LinkedInExample();
            RequestToken rt = example.requestUnauthorizedRequestToken();

            //Now that we have request token, let's authorize it....
            String url = example.getAuthorizationUrl(rt);

            //Copy the URL to your browser and make sure that OAuth 1 Servlet is running....
        } catch (OAuthException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

如您所见, CALLBACK_URL 设置为指向配置为JOAuth的OAuth Servlet,因为您将收到一个授权令牌.

As you can see, the CALLBACK_URL is set to point to the OAuth Servlet configured to JOAuth, as you'll receive an Authorized Token.

您必须确保通过 RequestToken getRequestToken(HttpServletRequest request) 方法将未经授权的请求令牌返回给服务,因为您需要它来检索访问令牌.

You must make sure that you return the unauthorized request token back to the service on the RequestToken getRequestToken(HttpServletRequest request) method as you'll need it to retrieve the access token.

LinkedIn返回访问令牌时,将调用服务 saveAccessToken()方法.您可以登录以验证是否返回了访问令牌.

The service saveAccessToken() method is called when LinkedIn returns an access token. You can log to verify if the access token is returned.

一旦有了访问令牌,就可以使用LinkedIn API并使用访问令牌发布作业.JOAuth旨在仅检索访问令牌,并且不与现有的其他API通信.

Once you have an access token, you can use a LinkedIn API and Post Jobs using the Access Token. JOAuth is designed to retrieve access tokens only and not communicate with other APIs that exists.

这篇关于使用JOAuth库在LinkedIn中发布职位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆