- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
现在,我用 dart 为 twitter 制作 OAuth 程序。
但是,我无法创建 oauth_nonce。
首先,我认为这段代码。
String create_nonce(){
var rnd = new Random();
int i = 0;
var number = rnd.nextInt(pow(2,8*4));
List<int> listi = UTF8.encode(number.toString());
String str = "";
while(i < 3){
number = rnd.nextInt(pow(2,8*4));
if(number < pow(2,8*4) - 1){
number = rnd.nextInt(pow(2,8*4));
}
listi = UTF8.encode(number.toString());
str = str + CryptoUtils.bytesToBase64(listi);
i++;
}
return str;
}
但是,该方法无法创建预期的字符串。
Please tell me how to make a oauth_nonce.
Nonce The oauth_nonce parameter is a unique token your application should generate for each unique request. Twitter will use this value to determine whether a request has been submitted multiple times. The value for this request was generated by base64 encoding 32 bytes of random data, and stripping out all non-word characters, but any approach which produces a relatively random alphanumeric string should be OK here.
oauth_nonce kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg
最佳答案
import 'dart:math' as math;
import 'package:crypto/crypto.dart';
void main() {
math.Random rnd = new math.Random();
List<int> values = new List<int>.generate(32, (i) => rnd.nextInt(256));
print(CryptoUtils.bytesToBase64(values));
}
产生
QqPlpI8BmDk9byWDqJ4tBCMMIWv24v4WL5KZsufnWqQ=
我不确定这到底意味着什么
and stripping out all non-word characters
我找到了https://dev.twitter.com/discussions/12445
You just want to make sure you're not sending characters like "!" "#" or "$" in your oauth_nonce. The process you've suggested sounds like it would work just fine.
.
Base64 includes '+' and '/'. You may need to stripping out them since they are non-word characters.
这样就可以了
import 'dart:math' as math;
import 'package:crypto/crypto.dart';
void main() {
math.Random rnd = new math.Random();
List<int> values = new List<int>.generate(32, (i) => rnd.nextInt(256));
print(CryptoUtils.bytesToBase64(values).replaceAll(new RegExp('[=/+]'), ''));
}
替换replaceAll
之前和之后
elrA+4rWr4O3zNv0L57iOLqTQD94abJ23hFoK+hk6QE=
elrA4rWr4O3zNv0L57iOLqTQD94abJ23hFoKhk6QE
关于twitter - 如何在 Dart 中设置 OAuth 随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24737409/
我是一名优秀的程序员,十分优秀!