<commit_before>// Copyright (c) 2015, <your name>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

import 'package:twitter/twitter.dart' as twitter;
import 'dart:async';
import 'dart:convert';
import 'package:oauth/oauth.dart' as oauth;
import 'dart:io';
import 'package:json_object/json_object.dart';

class TwitterKey {

  oauth.Token consumer;
  oauth.Token user;

  TwitterKey.createKey(String consumerKey,String consumerSecret,
                              String accessToken,String accessSecret) {

    this.consumer = createToken(consumerKey,consumerSecret);
    this.user = createToken(accessToken,accessSecret);
  }

}

oauth.Token createToken(String key, String secret) {
  return new oauth.Token(key, secret);
}



main() {
 
  // authorization string from https://dev.twitter.com/oauth/tools/signature-generator/7220915?nid=875
  var authorization = 'OAuth oauth_consumer_key="<KEY>", oauth_nonce="b3b3626a4c5806bcc8601d98fe7d0d51", oauth_signature="lare0FLb8SNyITNJlJYkXLLEKH8%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1426774910", oauth_token="<KEY>", oauth_version="1.0"';
  var url = "https://stream.twitter.com/1.1/statuses/sample.json";
  
  new HttpClient().getUrl(Uri.parse(url))
  
  .then((HttpClientRequest request) {
    // Prepare the request.
    request.headers.set('Authorization', authorization);
    request.headers.set('User-Agent', 'OAuth gem v0.4.4');
    request.headers.set('accept', '*/*');

    return request.close();
  })<commit_msg>Twitter authorization automated(crudely)<commit_after>// Copyright (c) 2015, <your name>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

import 'dart:convert';
import 'dart:io';
import 'dart:math' as math;
import 'package:crypto/crypto.dart' as crypto;
import 'dart:core';

String create_nonce(){
  math.Random rnd = new math.Random();
  List<int> values = new List<int>.generate(32, (i) => rnd.nextInt(256));
  var oauth_nonce = crypto.CryptoUtils.bytesToBase64(values).replaceAll(new RegExp('[=/+]'), '');
  return oauth_nonce;
}
String computeSignature(List<int> key, List<int> signatureBase) {
  var mac = new crypto.HMAC(new crypto.SHA1(), key);
  mac.add(signatureBase);
  return crypto.CryptoUtils.bytesToBase64(mac.close());
}

String getAuthorizationHeaders() {
  String oauth_consumer_key     = 'QSW4maKbx5jsWzDLxg48DPsO6';                            // Get Consumer Key from http://apps.twitter.com
    String oauth_consumer_secret  = '<KEY>'; // Get Consumer Secret from http://apps.twitter.com
    String oauth_token            = '<KEY>'; // Get Access Key from http://apps.twitter.com
    String oauth_access_secret    = '<KEY>';      // Get Access Secret from http://apps.twitter.com

    String oauth_method           = 'HMAC-SHA1';
    String oauth_version          = '1.0';
    String oauth_nonce            = create_nonce();
    String timestamp              = (new DateTime.now().millisecondsSinceEpoch ~/ 1000).toString();
   
    // A very crude way to create base string for oauth
    String base_string            = "GET&https%3A%2F%2Fstream.twitter.com%2F1.1%2Fstatuses%2Fsample.json&"
                                    +"oauth_consumer_key%3D"        + oauth_consumer_key
                                    +"%26oauth_nonce%3D"            + oauth_nonce
                                    +"%26oauth_signature_method%3D" + oauth_method
                                    +"%26oauth_timestamp%3D"        + timestamp
                                    +"%26oauth_token%3D"            + oauth_token 
                                    +"%26oauth_version%3D"          + oauth_version;
   
          
    String key                    = Uri.encodeComponent(oauth_consumer_secret) + '&' + Uri.encodeComponent(oauth_access_secret);
    String oauth_signature        = Uri.encodeComponent(computeSignature(UTF8.encode(key), UTF8.encode(base_string)));
     
    var authorization = 'OAuth oauth_consumer_key="' + oauth_consumer_key 
        +'", oauth_nonce="'                          + oauth_nonce
        +'", oauth_signature="'                      + oauth_signature
        +'", oauth_signature_method="'               + oauth_method
        +'", oauth_timestamp="'                      + timestamp
        +'", oauth_token="'                          + oauth_token
        +'", oauth_version="'                        + oauth_version
        +'"';
    
    return authorization;
}

main() {
 
  var authorization = getAuthorizationHeaders();
  // print(authorization);
  var url = "https://stream.twitter.com/1.1/statuses/sample.json";
  new HttpClient().getUrl(Uri.parse(url))
  
  .then((HttpClientRequest request) {
    // Prepare the request.
    request.headers.set('Authorization', authorization);
    request.headers.set('User-Agent', 'OAuth gem v0.4.4');
    request.headers.set('accept', '*/*');

    return request.close();
  })
{'fraction_non_alphanumeric': 0.12579872204472844, 'fraction_numerical': 0.022563897763578276, 'mean_word_length': 2.7048816568047336, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 15, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1915837', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1516, 'n_words': 303}