0

我正在尝试使用 Spring Boot 创建一个 Spring Social 应用程序,但我在启动它时遇到了问题。我在网上搜索过 Spring 社交文档和 Spring Social 示例项目。无论我尝试过什么都没有用,如果有人可以在这里帮助我,我正在徘徊。

LinkedIn控制器

    package com.example.demo;

    import org.springframework.social.connect.ConnectionRepository;
    import org.springframework.social.linkedin.api.LinkedIn;
    import org.springframework.social.linkedin.api.ProfileOperations;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;

    @Controller
    @RequestMapping("/linkedin")
    public class LinkedInController {

private static final String LINKEDIN_DISPLAY = "linkedin";
private static final String LINKEDIN_REDIRECT = "redirect:/connect/linkedin";

private LinkedIn linkedIn;
private ConnectionRepository connectionRepository;

public LinkedInController(LinkedIn linkedIn, ConnectionRepository connectionRepository) {
    this.linkedIn = linkedIn;

    this.connectionRepository = connectionRepository;
}

@GetMapping
public String helloLinkedIn(Model model) {
    if (connectionRepository.findPrimaryConnection(LinkedIn.class) == null) {
        return LINKEDIN_REDIRECT;

    }
    ProfileOperations user = linkedIn.profileOperations();
    System.out.println(user);
    model.addAttribute("linkedInProfile",linkedIn.profileOperations().getUserProfileFull());



    return LINKEDIN_DISPLAY;
}

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>HCLinkedIn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>HCLinkedIn</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <start-class>com.example.demo.HcLinkedInApplication</start-class>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.social</groupId>
        <artifactId>spring-social-linkedin</artifactId>
        <version>1.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.social</groupId>
        <artifactId>spring-social-core</artifactId>
        <version>1.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.social</groupId>
        <artifactId>spring-social-config</artifactId>
        <version>1.1.6.RELEASE</version>
    </dependency>
    <!-- <dependency>
        <groupId>org.springframework.social</groupId>
        <artifactId>spring-social-security</artifactId>
        <version>1.1.6.RELEASE</version>
    </dependency> -->

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/libs-milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/libs-milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

应用程序属性

spring.social.linkedin.app-id=<<client-key>>
spring.social.linkedin.app-secret=<<client-secret>>
4

1 回答 1

0

我有同样的问题,原来我忘了添加 OAuth2.0 重定向 url。

在linkedin 和OAuth 2.0标题下登录您的开发者帐户

重定向 url 将有一个名为Authorized Redirect URLs 的输入字段:

添加重定向 url ..假设您正在使用 localhost 进行测试,输入http://localhost:8080/connect/linkedin

将 8080 替换为您的端口,将 /connect/linkedin 替换为您用于访问授权的任何内容。

此外,如果您没有指定..将linkedinConnect.html 和linkedinConnected.html 添加到您的模板/连接

于 2018-11-05T10:20:00.903 回答