Giter VIP home page Giter VIP logo

scalascript-social's Introduction

ScalaScript: Social

Type-safe Scala.js Bindings for Social Networks (including Facebook and LinkedIn)

Introduction

ScalaScript-Social is a type-safe Scala.js binding for Social Networks; currently limited to Facebook and LinkedIn, but others will follow (including Google Plus and Twitter)

Development

Build Requirements

Building and installation

~\> sbt clean publish-local

Sample Code for Facebook

Inside of your HTML index page:

<script>
    (function(d, s, id){
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) {return;}
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook.net/en_US/sdk.js";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
</script>

Within your Scala.js application, you can initialize the Facebook SDK:

import com.github.ldaniels528.scalascript.social.facebook.Facebook.FB

val config = FacebookAppConfig(appId = "[YOUR_APP_KEY_GOES_HERE]", status = true, xfbml = true, version = "v2.5")
FB.init(config)

Assuming you're using AngularJS, within your Scala.js application, you define the AngularJS Facebook service:

module.serviceOf[FacebookService]("Facebook")

Finally, within your AngularJS controller or service you invoke the Facebook login:

import com.github.ldaniels528.scalascript.util.ScalaJsHelper._

class SocialController($scope: SocialControllerScope, @injected("Facebook") facebook: FacebookService) extends Controller {
    private var facebookID: js.UndefOr[String] = js.undefined

    $scope.loginToFacebook = () => {
        facebook.login() onComplete {
          case Success(response) =>
            facebookID = response.authResponse.userID
          case Failure(e) =>
            toaster.error("Facebook Login Error", e.displayMessage)
        }
    }   
}

@js.native
trait SocialControllerScope extends Scope {
    var loginToFacebook: js.Function0[Unit] = js.native
}

Afterwards, you may call any Facebook API that you have the permissions to execute:

import com.github.ldaniels528.scalascript.util.ScalaJsHelper._

val outcome = for {
  // load the user"s Facebook profile
  fbProfile <- facebook.getUserProfile
  fbFriends <- facebook.getTaggableFriends
} yield (fbProfile, friends)

outcome onComplete {
  case Success((fbProfile, friends)) =>
    console.log("fbProfile = ${angular.toJson(fbProfile, pretty = true)}")
    console.log(s"fbFriends = ${angular.toJson(fbFriends, pretty = true)}")
  case Failure(e) =>
    toaster.error(s"Failed to retrieve Facebook profile and friends - ${e.displayMessage}")
}
()

If you're not using AngularJS, you can use the Facebook SDK directly:

FB.login((response: js.UndefOr[FacebookLoginStatusResponse]) =>
  response.toOption match {
    case Some(resp) if resp.error.isEmpty =>
        console.log(s"auth = ${angular.toJson(auth)}")
    case Some(resp) => console.error(s"Error logging in to Facebook: ${resp.error}")
    case None => console.error("No response from Facebook servers")
  })     

Sample Code for LinkedIn

Inside of your HTML index page:

<script type="text/javascript" src="//platform.linkedin.com/in.js">
    api_key: [YOUR_API_KEY_GOES_HERE]
    authorize: true
    onLoad: linkedInInit
    scope: r_basicprofile r_emailaddress rw_company_admin w_share
</script>

Within your Scala.js application:

js.Dynamic.global.linkedInInit = () => {
  val injector = angular.element(jQuery("#Main")).injector()
  injector.get[MySessionService]("MySession").toOption match {
    case Some(mySession) =>
      console.info("Initializing LinkedIn API...")
      mySession.initLinkedIn(LinkedIn.IN)
    case None =>
      console.error("MySession could not be retrieved.")
  }
}

Afterwards, you may call any LinkedIn API that you have the permissions to execute:

import com.github.ldaniels528.scalascript.social.linkedin.LinkedIn.IN

var linkedInID: js.UndefOr[String] = js.undefined

// read the authenticated user's profile
IN.API.Profile(js.Array("me")) onComplete {
  case Success(response) =>
    linkedInID = response.values.headOption.flatMap(_.id.toOption).orUndefined
  case Failure(e) =>
    console.error(s"Failed to retrieve LinkedIn profile - ${e.getMessage}")
}

scalascript-social's People

Contributors

ldaniels528 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

linecomparison

scalascript-social's Issues

Add scope parameter to login function ?

Thank you for this great library! It got me up and running within few minutes.

Facebook documentation here mentions that we can also send scope parameters to the login function

FB.login(
  function(response) {
    console.log(response);
  },
  {
    scope: 'user_likes',
    auth_type: 'rerequest'
  }
);

I would like to add this. But just wanted to confirm is there is another way of sending scope variables to in facebook login for people who are not using Angularjs.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.