【问题标题】:Error message is (#12) bio field is deprecated for versions v2.8 and higher错误消息是 (#12) 版本 v2.8 及更高版本不推荐使用 bio 字段
【发布时间】:2017-02-14 21:32:01
【问题描述】:

我使用了 spring-social-facebook 和 Facebook app api v2.8 的 2.0.3.RELEASE 版本。 我打电话给 Facebook 登录,但返回了这条消息。 “(#12) 版本 v2.8 及更高版本不推荐使用 bio 字段” 我该如何解决这个问题?

【问题讨论】:

标签: spring-social-facebook


【解决方案1】:

我遇到了同样的错误,spring-social-facebook 的 2.0.3.RELEASE 似乎与 v2.8 Facebook API 版本(昨天发布)不兼容。从 facebook 更新日志中读取 v2.8 (https://developers.facebook.com/docs/apps/changelog):

用户履历 - 用户对象上的履历字段不再可用。如果为某人设置了 bio 字段,则该值现在将附加到 about 字段。

我认为我们必须等待 spring-social-facebook 库的新版本。在 2.0.3 版本中(在接口 org.springframework.social.facebook.api.UserOperations 中),PROFILE_FIELDS 常量中有“bio”字段,v2.8 facebook API 版本不支持。

更新:我在我的案例中找到了解决方法:

之前:

Connection<Facebook> connection = facebookConnectionFactory.createConnection(accessGrant);
Facebook facebook = connection.getApi();
User userProfile = facebook.userOperations().getUserProfile();//raises the exception caused by the "bio" field.

之后

Connection<Facebook> connection = facebookConnectionFactory.createConnection(accessGrant);
Facebook facebook = connection.getApi();
String [] fields = { "id", "email",  "first_name", "last_name" };
User userProfile = facebook.fetchObject("me", User.class, fields);

这里是您可以使用的字段的完整列表:

{ "id", "about", "age_range", "birthday", "context", "cover", "currency", "devices", "education", "email", "favorite_athletes", "favorite_teams", "first_name", "gender", "hometown", "inspirational_people", "installed", "install_type", "is_verified", "languages", "last_name", "link", "locale", "location", "meeting_for", "middle_name", "name", "name_format", "political", "quotes", "payment_pricepoints", "relationship_status", "religion", "security_settings", "significant_other", "sports", "test_group", "timezone", "third_party_id", "updated_time", "verified", "video_upload_limits", "viewer_can_send_gift", "website", "work"}

【讨论】:

  • 我收到上述错误,有什么建议吗? {"errors":["com.fasterxml.jackson.databind.JsonMappingException","无法构造 org.springframework.social.connect.UserProfile 的实例:找不到合适的构造函数,无法从 Object 值反序列化(缺少默认构造函数或创建者,或者可能需要添加/启用类型信息?)\n at [Source: java.io.ByteArrayInputStream@fa18e2e; line: 1, column: 2]"]}
  • 这个解决方案是facebook.userOperations().getUserProfile() 没有任何参数的方法的一个很好的替代品。但是,当我们想要获取其他配置文件 ID 的用户对象时,如何做同样的事情呢?例如,如何为facebook.userOperations().getUserProfile("some_facebook_id_here")做同样的事情
  • 请参阅stackoverflow.com/a/44872810/2015311,了解适用于所有情况的解决此问题的另一种方法。
  • 我不明白 - 你显示的这段代码应该去哪里?我正在关注the docs,我遇到了同样的错误 - 但是你在哪里对代码进行了这个特殊的更改?
  • 您可以找到 spring-social-facebook 当前文档here。在3.1 部分中,如果您使用 spring-social-facebook 的 2.0.3.RELEASE 和 facebook 应用程序 api 版本 >=v2.8,则会引发错误的代码。
【解决方案2】:

JHipster 的解决方法。将以下 sn-p 添加到您的 SocialService 类中,直到修复 spring-social-facebook

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import javax.annotation.PostConstruct;

@PostConstruct
private void init() {
    try {
        String[] fieldsToMap = { "id", "about", "age_range", "birthday",
                "context", "cover", "currency", "devices", "education",
                "email", "favorite_athletes", "favorite_teams",
                "first_name", "gender", "hometown", "inspirational_people",
                "installed", "install_type", "is_verified", "languages",
                "last_name", "link", "locale", "location", "meeting_for",
                "middle_name", "name", "name_format", "political",
                "quotes", "payment_pricepoints", "relationship_status",
                "religion", "security_settings", "significant_other",
                "sports", "test_group", "timezone", "third_party_id",
                "updated_time", "verified", "viewer_can_send_gift",
                "website", "work" };

        Field field = Class.forName(
                "org.springframework.social.facebook.api.UserOperations")
                .getDeclaredField("PROFILE_FIELDS");
        field.setAccessible(true);

        Field modifiers = field.getClass().getDeclaredField("modifiers");
        modifiers.setAccessible(true);
        modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
        field.set(null, fieldsToMap);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

来源:https://github.com/jhipster/generator-jhipster/issues/2349 - 在 fieldsToMap 数组中减去 bio

【讨论】:

【解决方案3】:
package hello;

import  org.springframework.social.connect.ConnectionRepository;
import  org.springframework.social.facebook.api.Facebook;
import  org.springframework.social.facebook.api.PagedList;
import  org.springframework.social.facebook.api.Post;
import  org.springframework.social.facebook.api.User;
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("/")
public class HelloController {

    private Facebook facebook;
    private ConnectionRepository connectionRepository;

    public HelloController(Facebook facebook, ConnectionRepository connectionRepository) {
        this.facebook = facebook;
        this.connectionRepository = connectionRepository;
    }

    @GetMapping
    public String helloFacebook(Model model) {
        if (connectionRepository.findPrimaryConnection(Facebook.class) == null) {
            return "redirect:/connect/facebook";
        }
        String [] fields = { "id","name","birthday","email","location","hometown","gender","first_name","last_name"};
        User user = facebook.fetchObject("me", User.class, fields);
        String name=user.getName();
        String birthday=user.getBirthday();
        String email=user.getEmail();
        String gender=user.getGender();
        String firstname=user.getFirstName();
        String lastname=user.getLastName();
        model.addAttribute("name",name );
        model.addAttribute("birthday",birthday );
        model.addAttribute("email",email );
        model.addAttribute("gender",gender);
        model.addAttribute("firstname",firstname);
        model.addAttribute("lastname",lastname);
        model.addAttribute("facebookProfile", facebook.fetchObject("me", User.class, fields));
        PagedList<Post> feed = facebook.feedOperations().getFeed();
        model.addAttribute("feed", feed);
        return "hello";
    }

}

【讨论】:

  • 您是如何取回这些字段数据的?我只能得到名字,名字和姓氏,其他的都是空的。我特别需要找回朋友的数据。所以,它也是空的。
【解决方案4】:

这已在 spring-social-facebook 的新版本中得到修复。请将以下内容添加到您的 pom.xml 中

<dependency>
    <groupId>org.springframework.social</groupId>
    <artifactId>spring-social-facebook</artifactId>
    <version>3.0.0.M1</version>
</dependency>

如果您收到此版本不可用的错误,请同时添加以下内容。

<repositories>
    <repository>
        <id>alfresco-public</id>
        <url>https://artifacts.alfresco.com/nexus/content/groups/public</url>
    </repository>
</repositories>

【讨论】:

  • 谢谢...事件操作怎么样?你有什么知识吗。 org.springframework.social.UncategorizedApiException: (#12) is_date_only 字段已弃用 v2.4 及更高版本]
  • 这不再适合我。我不得不使用 spring 里程碑存储库:
  • 查看这篇文章,了解使用 3.0.0.M1 stackoverflow.com/a/42175408/1410212 再次获取 ConnectionRepository 所需的其他依赖项
【解决方案5】:

在 grails spring security facebook 下,我遇到了类似的问题,并且 感谢@user6904265,我设法让它工作了:

//This was provided example method:
//org.springframework.social.facebook.api.User fbProfile = facebook.userOperations().userProfile
//This is the groovy way of declaring fields:
String[] fields = ['id',"email", "age_range", "birthday","first_name",
                    "last_name","gender"]  as String[]
//This bit pay attention to the User.class segment. 
org.springframework.social.facebook.api.User fbProfile = 
facebook.fetchObject("me", 
org.springframework.social.facebook.api.User.class, fields)

基本上,上面提供的默认示例状态为User.class。本地运行无法找到诸如 last_name 等字段,并提供了一个可以查询的列表。这些提供的选项来自实际的 spring security User 类(默认为我的应用程序),因此请确保您还查找正确的用户类。

【讨论】:

    【解决方案6】:
    FacebookTemplate template = new FacebookTemplate(access_token); 
    String [] fields = { "id", "email",  "first_name", "last_name" };
    User profile = template.fetchObject("me", User.class, fields);
    

    【讨论】:

      【解决方案7】:

      我在使用新版本的 spring-social-facebook 时遇到了问题。要使用版本 2.0.3.RELEASE 修复此问题,请将以下代码粘贴到您的 SocialService.java

      @PostConstruct
      private void init() {
          try {
              String[] fieldsToMap = {
                  "id", "about", "age_range", "birthday", "context", "cover", "currency", "devices", "education", "email", "favorite_athletes", "favorite_teams", "first_name", "gender", "hometown", "inspirational_people", "installed", "install_type","is_verified", "languages", "last_name", "link", "locale", "location", "meeting_for", "middle_name", "name", "name_format","political", "quotes", "payment_pricepoints", "relationship_status", "religion", "security_settings", "significant_other","sports", "test_group", "timezone", "third_party_id", "updated_time", "verified", "viewer_can_send_gift","website", "work"
              };
      
              Field field = Class.forName("org.springframework.social.facebook.api.UserOperations").
                      getDeclaredField("PROFILE_FIELDS");
              field.setAccessible(true);
      
              Field modifiers = field.getClass().getDeclaredField("modifiers");
              modifiers.setAccessible(true);
              modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
              field.set(null, fieldsToMap);
      
          } catch (Exception ex) {
              ex.printStackTrace();
          }
      }
      

      此代码不会尝试从 Facebook 检索 bio 字段。

      您可以在此处查看更多详细信息:https://github.com/jhipster/generator-jhipster/issues/2349

      【讨论】:

      • 超级!这工作得很好。我认为这应该是这个问题的合适答案,因为这不仅解决了facebook.userOperations().getUserProfile() 的一种情况的问题,而且还解决了由于bio 字段被停用而导致的所有其他问题。感谢您的解决方案!
      【解决方案8】:
      【解决方案9】:

      我在我的项目中复制了界面 UserOperations,更改了 PROFILE_FIELDS 删除了 bio 字段。在 Tomcat 上,我编辑的类优先于 org.springframework.social.facebook.api.UserOperations 接口,这解决了问题。 (见下面文件的最后一行)

      /*
       * Copyright 2015 the original author or authors.
       *
       * Licensed under the Apache License, Version 2.0 (the "License");
       * you may not use this file except in compliance with the License.
       * You may obtain a copy of the License at
       *
       *      http://www.apache.org/licenses/LICENSE-2.0
       *
       * Unless required by applicable law or agreed to in writing, software
       * distributed under the License is distributed on an "AS IS" BASIS,
       * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       * See the License for the specific language governing permissions and
       * limitations under the License.
       */
      package org.springframework.social.facebook.api;
      
      import java.util.List;
      
      import org.springframework.social.ApiException;
      import org.springframework.social.MissingAuthorizationException;
      public interface UserOperations {
      
      /**
       * Retrieves the profile for the authenticated user.
       * @return the user's profile information.
       * @throws ApiException if there is an error while communicating with Facebook.
       * @throws MissingAuthorizationException if FacebookTemplate was not created with an access token.
       */
      User getUserProfile();
      
      /**
       * Retrieves the profile for the specified user.
       * @param userId the Facebook user ID to retrieve profile data for.
       * @return the user's profile information.
       * @throws ApiException if there is an error while communicating with Facebook.
       */
      User getUserProfile(String userId);
      
      /**
       * Retrieves the user's profile image. Returns the image in Facebook's "normal" type.
       * @return an array of bytes containing the user's profile image.
       * @throws ApiException if there is an error while communicating with Facebook.
       * @throws MissingAuthorizationException if FacebookTemplate was not created with an access token.
       */
      byte[] getUserProfileImage();
      
      /**
       * Retrieves the user's profile image. Returns the image in Facebook's "normal" type.
       * @param userId the Facebook user ID.
       * @return an array of bytes containing the user's profile image.
       * @throws ApiException if there is an error while communicating with Facebook.
       */
      byte[] getUserProfileImage(String userId);
      
      /**
       * Retrieves the user's profile image.
       * @param imageType the image type (eg., small, normal, large. square)
       * @return an array of bytes containing the user's profile image.
       * @throws ApiException if there is an error while communicating with Facebook.
       * @throws MissingAuthorizationException if FacebookTemplate was not created with an access token.
       */
      byte[] getUserProfileImage(ImageType imageType);
      
      /**
       * Retrieves the user's profile image.
       * @param userId the Facebook user ID.
       * @param imageType the image type (eg., small, normal, large. square)
       * @return an array of bytes containing the user's profile image.
       * @throws ApiException if there is an error while communicating with Facebook.
       */
      byte[] getUserProfileImage(String userId, ImageType imageType);
      
      /**
       * Retrieves the user's profile image. When height and width are both used,
       * the image will be scaled as close to the dimensions as possible and then
       * cropped down.
       * @param width the desired image width
       * @param height the desired image height
       * @return an array of bytes containing the user's profile image.
       * @throws ApiException if there is an error while communicating with Facebook.
       */
      byte[] getUserProfileImage(Integer width, Integer height);
      
      /**
       * Retrieves the user's profile image. When height and width are both used,
       * the image will be scaled as close to the dimensions as possible and then
       * cropped down.
       * @param userId the Facebook user ID.
       * @param width the desired image width
       * @param height the desired image height
       * @return an array of bytes containing the user's profile image.
       * @throws ApiException if there is an error while communicating with Facebook.
       */
      byte[] getUserProfileImage(String userId, Integer width, Integer height);
      
      /**
       * Retrieves a list of permissions that the application has been granted for the authenticated user.
       * @return the permissions granted for the user.
       * @throws ApiException if there is an error while communicating with Facebook.
       * @throws MissingAuthorizationException if FacebookTemplate was not created with an access token.
       */
      List<Permission> getUserPermissions();
      
      /**
       * Fetches IDs that the user has on any applications associated with the calling application via Facebook's Business Mapping API.
       * @return a list of ID-to-application mapping that the user has on related applications.
       */
      List<UserIdForApp> getIdsForBusiness();
      
      /**
       * Fetches a list of places that the user has checked into or has been tagged at.
       * @return a list of place tags for the user.
       */
      List<PlaceTag> getTaggedPlaces();
      
      /**
       * Searches for users.
       * @param query the search query (e.g., "Michael Scott")
       * @return a list of {@link Reference}s, each representing a user who matched the given query.
       * @throws ApiException if there is an error while communicating with Facebook.
       * @throws MissingAuthorizationException if FacebookTemplate was not created with an access token.
       */
      PagedList<Reference> search(String query);
      
      static final String[] PROFILE_FIELDS = {
          "id", "about", "age_range",/*"bio",*/ "birthday", "context", "cover", "currency", "devices", "education", "email", 
          "favorite_athletes", "favorite_teams", "first_name", "gender", "hometown", "inspirational_people", "installed", "install_type",
          "is_verified", "languages", "last_name", "link", "locale", "location", "meeting_for", "middle_name", "name", "name_format", 
          "political", "quotes", "payment_pricepoints", "relationship_status", "religion", "security_settings", "significant_other", 
          "sports", "test_group", "timezone", "third_party_id", "updated_time", "verified", "video_upload_limits", "viewer_can_send_gift", 
          "website", "work"
      };
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-12-07
        • 2020-07-18
        • 2015-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多