5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Wordpressのfunctions.phpのみでfacebook OAuthを実装する方法。

Last updated at Posted at 2018-01-10

Facebook APIを利用してOAuth2を経由、メールアドレスなどを取得したい場合、Facebook SDKなどを利用し取得するのですが、色々とライブラリ読み込ませたりと面倒くさかったのでイチから書いたは非常にスムーズでした。

WordpressでOAuth2のプロセスのコードを載っけておきます。面倒くさがりなのでfile_get_contents()でhttp(s)リクエスト送っていますがcurlでしっかりとリクエスト送ってあげてください。

functions.php

<?php

function get_facebook_app_id()
{
	/* https://developers.facebook.com/apps/ */
	return '123456789012345';
}
function get_facebook_redirect_uri()
{
	/* URI of a page which has been called get_facebook_graph() function */
	return 'https://example.com/oauth';
}
function get_facebook_api_version()
{
	/* https://developers.facebook.com/docs/graph-api/changelog */
	return 'v2.11';
}
function get_facebook_scope()
{
	/* https://developers.facebook.com/docs/facebook-login/permissions/ */
	return 'email';
}
function get_facebook_fields()
{
	/* https://developers.facebook.com/docs/graph-api/reference/user/ */
	return 'id,name,email,picture';
}
function get_facebook_client_secret()
{
	/* https://developers.facebook.com/apps/ -> your app -> dash board */
	return '123456789012345678901234567890';
}
function get_oauth_error_uri()
{
	/* error URI which you prepare on your wp */
	return 'https://example.com/error';
}
function get_facebook_dialog_uri()
{
	return 'https://www.facebook.com/'.get_facebook_api_version().'/dialog/oauth?'.
			'client_id='.get_facebook_app_id().'&'.
			'redirect_uri='.urlencode( get_facebook_redirect_uri() ).'&'.
			'scope='.get_facebook_scope().'&'.
			'state='.sha1(session_id())
	;
}
function get_facebook_graph()
{
	$state = $_GET['state'];
	
	if($state != sha1(session_id())) exit;
	
	$error = $_GET['error'];	
	$error_description = $_GET['error_description'];
	$code = $_GET['code'];
	
	if($error or !$code)
	{
		header('Location: '.get_oauth_error_uri().
				'?error='.urldecode($error).
				'&error_description'.urlencode($error_description)
		);
		exit;
	}
	
	$url = 	'https://graph.facebook.com/'.get_facebook_api_version().'/oauth/access_token?'.
			'&client_id='.get_facebook_app_id().
			'&redirect_uri='.get_facebook_redirect_uri().
			'&client_secret='.get_facebook_client_secret().
			'&code='.$code
	;
	$result_json = file_get_contents($url);
	$result = json_decode( $result_json ); // access by class
	
	$url = 'https://graph.facebook.com/'.get_facebook_api_version().'/me?fields='.get_facebook_fields().'&access_token='.$result->access_token;
	
	$result_json = file_get_contents($url);
	return json_decode( $result_json );
	
}

Facebookでログインというボタンをつけるページ
index.phpやpage.phpなどのページからの呼び出し。

<a target="_blank" href="<?php echo get_facebook_dialog_uri() ?>">Facebookにログイン</a>

OAuth2を経由してFacebook graphの内容を取得。Classでのアクセス。get_facebook_scope(), get_facebook_fields()の値で取得できる内容が変わります。詳細はfacebook scope, fieldsを参照してください。
https://developers.facebook.com/docs/facebook-login/permissions/
https://developers.facebook.com/docs/graph-api/reference/user/

<?php
  $graph_vars = get_facebook_graph();
/*
stdClass Object
(
    [id] => 1234567890
    [name] => Example Name
    [email] => name@example.com
    [picture] => stdClass Object
        (
            [data] => stdClass Object
                (
                    [height] => __
                    [is_silhouette] => __
                    [url] => __
                    [width] => __
                )
        )
)
*/
?>

Good Luck.

5
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?