gpt4 book ai didi

twitter - 如何在 Dart 中设置 OAuth 随机数

转载 作者:行者123 更新时间:2023-12-02 17:25:00 27 4
gpt4 key购买 nike

现在,我用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com