2 回答
TA貢獻1865條經驗 獲得超7個贊
我創建了一個有效的要點。一些建議:
所有值都必須是 urlenconde()
您需要用于簽名請求的字符串需要包括:方法(大寫)和 URL(urlcode)和參數列表(url 編碼)
簽名密鑰是您的消費者密鑰(urlencode),后跟 &。這是因為通常您的簽名密鑰需要我的消費者秘密和秘密令牌。在這種情況下,我們沒有秘密令牌
對于您的簽名基本字符串的參數列表,您需要包括所有 oauth_ 參數和您的 grant_type,因為您還需要包括 body 參數。這些鍵的排序至關重要
一些代碼PHP代碼:
$keyId = getenv('HERE_API_KEY_ID');
$keySecret = getenv('HERE_API_KEY_SECRET');
$httpBody = [
"grant_type" => "client_credentials"
];
$httpMethod = "POST";
$httpUrl = 'https://account.api.here.com/oauth2/token';
$oauthNonce = mt_rand();
$oauthTimestamp = time();
$oauthSignatureMethod= "HMAC-SHA256";
$oauthVersion = "1.0";
$baseString = $httpMethod."&". urlencode($httpUrl);
$oauth1Param = [
'oauth_consumer_key' => $keyId,
'oauth_signature_method' => $oauthSignatureMethod,
'oauth_timestamp' => $oauthTimestamp,
'oauth_nonce' => $oauthNonce,
'oauth_version' => $oauthVersion
];
$paramString =
"grant_type=client_credentials&".
"oauth_consumer_key=". urlencode($oauth1Param['oauth_consumer_key']).
"&oauth_nonce=". urlencode($oauth1Param['oauth_nonce']).
"&oauth_signature_method=". urlencode($oauth1Param['oauth_signature_method']).
"&oauth_timestamp=". urlencode($oauth1Param['oauth_timestamp']).
// "&oauth_token=".
"&oauth_version=". urlencode($oauth1Param['oauth_version'])
;
echo $paramString.PHP_EOL;
$baseString = $baseString . "&" . urlencode($paramString);
echo $baseString . PHP_EOL;
$signingKey= urlencode($keySecret) . "&";
$signature = urlencode(
base64_encode(
hash_hmac(
'sha256',
$baseString,
$signingKey,
true
)
)
);
$oauth1Param['oauth_signature'] = $signature;
echo "RUNTIME SIGNATURE : " . $signature .PHP_EOL;
我創建了一個有效的 GIST,您唯一需要更改的是 $keyId 和 $keySecret。 https://gist.github.com/roberto-butti/736c38c796ede70c719f6a21a752c971
該文檔非常有用:https ://developer.twitter.com/en/docs/basics/authentication/oauth-1-0a/creating-a-signature
TA貢獻1829條經驗 獲得超7個贊
似乎 access_id 和 access_key 都在請求正文中傳遞,"grant_type" => "client_credentials"只需要在正文中,而access_id and access_key應該在標頭中傳遞。您可以先在郵遞員中嘗試,如果成功,請恢復
- 2 回答
- 0 關注
- 176 瀏覽
添加回答
舉報
