【问题标题】:Angularjs Satellizer not returning emailAngularjs Satellizer 不返回电子邮件
【发布时间】:2016-01-14 15:39:26
【问题描述】:

我有一个基于 angularjs 和 laravel 的应用程序,我使用 Satellizer 进行身份验证。

目前登录工作,但它只返回显示名称。代码如下:

satellizer.js

providers: {
        facebook: {
          name: 'facebook',
          url: '/auth/facebook',
          authorizationEndpoint: 'https://www.facebook.com/v2.3/dialog/oauth',
          redirectUri: (window.location.origin || window.location.protocol + '//' + window.location.host) + '/',
          requiredUrlParams: ['scope'],
          scope: ['email'],
          scopeDelimiter: ',',

          display: 'popup',
          type: '2.0',
          popupOptions: { width: 580, height: 400 }
        },

account.js

angular.module('MyApp')
  .factory('Account', function($http) {
    return {
      getProfile: function() {
        return $http.get('/api/me');
      },
      updateProfile: function(profileData) {
        return $http.put('/api/me', profileData);
      }
    };
  });

profile.js

<div class="container">
  <div class="panel panel-default">
    <div class="panel-heading">Profile</div>
    <div class="panel-body">
      <legend><i class="ion-clipboard"></i> Edit My Profile</legend>
      <form method="post" ng-submit="updateProfile()">
        <div class="form-group">
          <label class="control-label">Profile Picture</label>
          <img class="profile-picture" ng-src="{{user.picture || 'http://placehold.it/100x100'}}">
        </div>
        <div class="form-group">
          <label class="control-label"><i class="ion-person"></i> Display Name</label>
          <input type="text" class="form-control" ng-model="user.displayName" />
        </div>
        <div class="form-group">
          <label class="control-label"><i class="ion-at"></i> Email Address</label>
          <input type="email" class="form-control" ng-model="user.email" />
        </div>
        <button class="btn btn-lg btn-success">Update Information</button>
      </form>
    </div>
  </div>

laravel php 中的认证控制器

public function facebook(Request $request)
    {
        $accessTokenUrl = 'https://graph.facebook.com/v2.3/oauth/access_token';
        $graphApiUrl = 'https://graph.facebook.com/v2.3/me';

        $params = [
            'code' => $request->input('code'),
            'client_id' => $request->input('clientId'),
            'redirect_uri' => $request->input('redirectUri'),
            'client_secret' => Config::get('app.facebook_secret')

        ];

        $client = new GuzzleHttp\Client();

        // Step 1. Exchange authorization code for access token.
        $accessToken = $client->get($accessTokenUrl, ['query' => $params])->json();

        // Step 2. Retrieve profile information about the current user.
        $profile = $client->get($graphApiUrl, ['query' => $accessToken])->json();


        // Step 3a. If user is already signed in then link accounts.
        if ($request->header('Authorization'))
        {
            $user = User::where('facebook', '=', $profile['id']);

            if ($user->first())
            {
                return response()->json(['message' => 'There is already a Facebook account that belongs to you'], 409);
            }

            $token = explode(' ', $request->header('Authorization'))[1];
            $payload = (array) JWT::decode($token, Config::get('app.token_secret'), array('HS256'));

            $user = User::find($payload['sub']);
            dd($user);
            $user->facebook = $profile['id'];
            $user->displayName = $user->displayName || $profile['name'];
            $user->save();

            return response()->json(['token' => $this->createToken($user)]);
        }
        // Step 3b. Create a new user account or return an existing one.
        else
        {
            $user = User::where('facebook', '=', $profile['id']);

            if ($user->first())
            {
                return response()->json(['token' => $this->createToken($user->first())]);
            }

            $user = new User;

            $user->facebook = $profile['id'];
            $user->displayName = $profile['name'];
            $user->save();

            return response()->json(['token' => $this->createToken($user)]);
        }
    }

谢谢!

【问题讨论】:

标签: javascript php angularjs facebook-graph-api laravel-5


【解决方案1】:

在新的 Facebook Graph API 中,您应该在 Graph Api url 中包含电子邮件和其他您想要获取的内容,看看这个:https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5

因此,在您的情况下,解决方案是像这样更新您的 Api 网址:

更新:

authorizationEndpoint: 'https://www.facebook.com/v2.3/dialog/oauth',

与:

authorizationEndpoint: 'https://www.facebook.com/v2.5/dialog/oauth',

并更新:

$accessTokenUrl = 'https://graph.facebook.com/v2.3/oauth/access_token';
$graphApiUrl = 'https://graph.facebook.com/v2.3/me';

有了这个:

$accessTokenUrl = 'https://graph.facebook.com/v2.5/oauth/access_token';
$graphApiUrl = 'https://graph.facebook.com/v2.5/me?fields=id,name,email,picture,first_name,last_name';

【讨论】:

    猜你喜欢
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 2017-09-29
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多