简体   繁体   English

如何处理Twitter的Digits API

[英]How to Handle Twitter Digits API for Web

I'm working on twitter digits api to integrate it to my website which needs to verify user's uniqueness. 我正在使用twitter数字api将它集成到我的网站,需要验证用户的唯一性。

Here is a link , it's the only article which illustrate officially how to implement digits for web. 这是一个链接 ,它是唯一正式说明如何实现网络数字的文章。

In the article, I find the fact that I have to care for web server unlike Digits for IOS. 在文章中,我发现我不得不关心Web服务器,而不像Digits for IOS。 But NO INFORMATION about What should I do on my web server! 但是没有关于我应该在我的网络服务器上做什么的信息!

What should I write in PHP for server side programming to obtain user ID and Phone Number?? 我应该在PHP中编写什么来进行服务器端编程以获取用户ID和电话号码?

In the demo, http://s.codepen.io/digits/debug/gbrgYV 在演示中, http://s.codepen.io/digits/debug/gbrgYV

after you log in, it shows a curl command. 登录后,它会显示一个curl命令。

Use the response data to reproduce it in your server side and it will give you a response with the phone number and the ID. 使用响应数据在服务器端重现它,它将为您提供电话号码和ID的响应。

Although, i don't know why, when the phone number is new, it takes a while to return you the phone number. 虽然,我不知道为什么,当电话号码是新的时,需要一段时间才能返回电话号码。

Okay, here is the full implementation: 好的,这是完整的实现:

Js JS

//include js
<script type="text/javascript" id="digits-sdk" src="https://cdn.digits.com/1/sdk.js" async></script>
<script>
/* Initialize Digits for Web using your application's consumer key that Fabric generated */
    document.getElementById('digits-sdk').onload = function() {
        Digits.init({ consumerKey: '*********' });
    };

    /* Launch the Login to Digits flow. */
    function onLoginButtonClick(phone_number=''){
        if(phone_number!='')
        {
            //Digits.logIn({
            Digits.embed({
                phoneNumber : phone_number,
                container : '.my-digits-container'  //remove this if u will use Digits.logIn
            })
            .done(onLogin) /*handle the response*/
            .fail(onLoginFailure);
        }   
    }

    /* Validate and log use in. */
    function onLogin(loginResponse){
        // Send headers to your server and validate user by calling Digits’ API
        var oAuthHeaders = loginResponse.oauth_echo_headers;
        var verifyData = {
            authHeader: oAuthHeaders['X-Verify-Credentials-Authorization'],
            apiUrl: oAuthHeaders['X-Auth-Service-Provider'],
        };

        var request = $.ajax({
            url: "<?php echo $url;?>",
            method: "POST",
            dataType: "json",
            data: {
                verifyData:verifyData,
            }
        });
        request.done(function (data) {               
            console.log(data);
        });
        request.fail(function (jqXHR, textStatus) {
            alert('fail');
        });
    }

    function onLoginFailure(loginResponse){           
        alert('Something went wrong, Please try again');
    }
</script>

PHP Code PHP代码

Now you have a URL and header from digits then you can use below code: 现在您有一个数字的URL和标题,然后您可以使用下面的代码:

$apiUrl = 'https://api.digits.com/1.1/sdk/account.json';
$authHeader = 'OAuth oauth_consumer_key="**********", oauth_nonce="****", oauth_signature="****", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1481554529", oauth_token="*****", oauth_version="1.0"';

// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Authorization: {$authHeader}"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents($apiUrl, false, $context);

$final_output = array();
if($file)
{
    $final_output = json_decode($file,true);
}
print_r($final_output);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM