How to Handle Twitter Digits API for Web

蓝咒 提交于 2019-12-07 02:34:01

问题


I'm working on twitter digits api to integrate it to my website which needs to verify user's uniqueness.

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. 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??


回答1:


In the demo, http://s.codepen.io/digits/debug/gbrgYV

after you log in, it shows a curl command.

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.

Although, i don't know why, when the phone number is new, it takes a while to return you the phone number.




回答2:


Okay, here is the full implementation:

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

Now you have a URL and header from digits then you can use below code:

$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);


来源:https://stackoverflow.com/questions/28792718/how-to-handle-twitter-digits-api-for-web

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!