如何将博客文章中的图片附加到LinkedIn共享

How do you attach a picture to LinkedIn share from a blog post?

本文关键字:共享 LinkedIn 文章      更新时间:2023-09-26

我有一个博客,我想通过LinkedIn共享。LinkedIn提供的文档虽然只是简单地说,但没有足够的细节让我理解我的用例。我的用例要求我在每个博客文章中动态地放置图片和描述,而现在还没有填充。这是一个Angular项目。

我的当前代码:post.html

<script>
  delete IN;
  $.getScript("https://platform.linkedin.com/in.js");
</script>
<script type="IN/Share" data-url={{webAddress}} data-counter="right"></script>

post.js//我在这个区域的$scope变量中有我的所有数据,其中包括//我想把图片和描述附在帖子上。

以下是LinkedIn文档显示的正确方法:post.html

<script type="text/javascript" src="//platform.linkedin.com/in.js">
  api_key: YOUR_API_KEY_HERE
  authorize: true
  onLoad: onLinkedInLoad
</script>
<script type="text/javascript">
  // Setup an event listener to make an API call once auth is complete
    function onLinkedInLoad() {
      IN.Event.on(IN, "auth", shareContent);
    }
  // Handle the successful return from the API call
  function onSuccess(data) {
    console.log(data);
  }
  // Handle an error response from the API call
  function onError(error) {
    console.log(error);
  }
  // Use the API call wrapper to share content on LinkedIn
  function shareContent() {
    // Build the JSON payload containing the content to be shared
    var payload = { 
      "comment": "Check out developer.linkedin.com! http://linkd.in/1FC2PyG", 
      "visibility": { 
        "code": "anyone"
      } 
    };
    IN.API.Raw("/people/~/shares?format=json")
      .method("POST")
      .body(JSON.stringify(payload))
      .result(onSuccess)
      .error(onError);
  }
</script>

据我所知,我需要用正确的数据/链接填充有效负载对象。根据文档中的内容,我不知道如何做到这一点。

以下是我尝试/思考过的一些事情,以及我目前的困境:

1) 从post.js中获取数据,并将其放在post.html中脚本标记之间的payload对象中。经过一些研究,这是不可能做到的。虽然如果我错了,我欢迎被纠正。

2) 将IN对象放入angular中,并在post.js中填充有效负载。这听起来很好,但LinkedIn没有提供用angular调用post.js函数的html。此外,所提供的LinkedIn代码负责按钮的格式设置以及点击后的内容。

3) 使用JQuery在脚本标记中进行http调用。我很少使用JQuery,以前也从未使用过JQuery的http。如果这是一种可行的方法来思考这个问题,这就是我想到的:

<script type="IN/Share" data-url={{webAddress}} data-counter="right">
                  $.get( "https://public-api.wordpress.com/rest/v1.1/sites/myPost", function( response ) {
                    var post = _.first(_.filter(response.posts, function(n){return n.title.replace(/ /g,"-").replace(/[:]/g, "").toLowerCase() === $stateParams.id}));
                    var post1 = _.assign(post, {category: _.first(_.keys(post.categories)), pic: _.first(_.values(post.attachments)).URL, credit: _.first(_.values(post.attachments)).caption, linkCredit: _.first(_.values(post.attachments)).alt, fullStory: post.content.replace(/<(?!'s*'/?'s*p'b)[^>]*>/gi,'')});
                    **var image = post1.pic;**
                    **var title = post1.title;**
                    **var webAddress = window.location.href;**
                    function onLinkedInLoad() {
                      IN.Event.on(IN, "auth", shareContent);
                    }
                   function onSuccess(data) {
                    console.log(data);
                   }
                   function onError(error) {
                    console.log(error);
                   }
                   function shareContent(title, image, webAddress) {
                    var payload = {                         
                      "content": {
                        "title": title,
                        "submitted-image-url": image,
                        "submitted-url": webAddress
                      }
                    };
                    IN.API.Raw("/people/~/shares?format=json")
                      .method("POST")
                      .body(JSON.stringify(payload))
                      .result(onSuccess)
                      .error(onError);
                   }
                  });
                  </script>

这个解决方案也没有产生解决方案。从这里到哪里去,我不知道。我相信这很简单,但很特别,我需要一点帮助。

很遗憾,我没有使用过linkedin API

以我为例,也许不是所有的事情都是对的。但我必须在angular中使用一个变量IN,并编写关于调用API包装器的内容。

插件的使用示例,请参阅LinkedIn插件页面。

jsfiddle上的实例。

  //CallBackHell
  function LinkedInServiceFunc(callback) {
    callback && IN.Event.onDOMReady(callback);
  }
  angular.module('ExampleApp', [])
    .controller('ExampleController', function($scope, LinkedInService, ShareLinkedINService) {
      console.log('ExampleController IN', IN);
      console.log('ExampleController LinkedInService', LinkedInService);
      LinkedInService.promise.then(function(LIN) {
        console.log('Complete loading script for LinkedIn in ExampleController', LIN.Objects)
      });
      //Then you can interact with IN object as angular service. Like this
      $scope.shareContent = function() { // Use the API call wrapper to share content on LinkedIn
        // Build the JSON payload containing the content to be shared
        var payload = {
          "comment": $scope.comment,
          "visibility": {
            "code": 'anyone'
          }
        };
        // Handle the successful return from the API call
        function onSuccess(data) {
          console.log(data);
        }
        // Handle an error response from the API call
        function onError(error) {
          console.log(error);
        }
        console.log('shareContent', payload);
        LinkedInService.promise.then(function(LIN) {
          LIN.API.Raw("/people/~/shares?format=json")
            .method("POST")
            .body(JSON.stringify(payload))
            .result(onSuccess)
            .error(onError);
        });
      }
      $scope.shareContentService = function() {
        //It's better way, i think
        ShareLinkedINService.shareContent($scope.comment, 'anyone').then(function(data) {
          console.log('success', data);
        }).catch(function(data) {
          console.err('error', data);
        });
      }
    })
    .service('LinkedInService', function($q) {
      var defer = $q.defer();
      LinkedInServiceFunc(function() {
        defer.resolve(IN);
      });
      return {
        promise: defer.promise
      };
    })
    //You can create wrapper on IN API
    .service('ShareLinkedINService', function(LinkedInService, $q) {
      return {
        shareContent: function(comment, visible) {
          var defer = $q.defer();
          var payload = {
            "comment": comment,
            "visibility": {
              "code": visible
            }
          };
          LinkedInService.promise.then(function(LIN) {
            LIN.API.Raw("/people/~/shares?format=json")
              .method("POST")
              .body(JSON.stringify(payload))
              .result(defer.resolve)
              .error(defer.reject);
          });
          return defer.promise;
        }
      }
    })
    .directive('linkedInShareButton', function(LinkedInService) {
      return {
        restrict: "E",
        replace: false,
        scope: {
          shareUrl: "@",
          counter:"@"
        },
        link: function(scope, elem, attr) {
          var script = document.createElement('script');
          script.setAttribute('type', 'IN/Share');
          script.setAttribute('data-url', scope.shareUrl);
          script.setAttribute('data-counter', scope.counter);
          elem.append(script);
        },
      };
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script type="text/javascript" src="//platform.linkedin.com/in.js">
  authorize: false
  onLoad: LinkedInServiceFunc
    //I don't have api_key, because i delete it
    // api_key: YOUR_API_KEY_HERE
    // authorize: true
    // onLoad: onLinkedInLoad
</script>
<body ng-app="ExampleApp">
  <div>
    <div ng-controller="ExampleController">
      <input ng-model="comment">
      <button ng-click="shareContent()">
        shareContent
      </button>
      <button ng-click="shareContentService()">
        shareContentService
      </button>
      <script type="IN/Share" data-url="www.mail.ru" data-counter="top"></script>
      <linked-in-share-button share-url="www.mail.ru" counter="top"></linked-in-share-button>
    </div>
  </div>
</body>