簡體   English   中英

如何處理Twitter的Digits API

[英]How to Handle Twitter Digits API for Web

我正在使用twitter數字api將它集成到我的網站,需要驗證用戶的唯一性。

這是一個鏈接 ,它是唯一正式說明如何實現網絡數字的文章。

在文章中,我發現我不得不關心Web服務器,而不像Digits for IOS。 但是沒有關於我應該在我的網絡服務器上做什么的信息!

我應該在PHP中編寫什么來進行服務器端編程以獲取用戶ID和電話號碼?

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

登錄后,它會顯示一個curl命令。

使用響應數據在服務器端重現它,它將為您提供電話號碼和ID的響應。

雖然,我不知道為什么,當電話號碼是新的時,需要一段時間才能返回電話號碼。

好的,這是完整的實現:

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代碼

現在您有一個數字的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