2012-12-20 107 views
6

我试图通过直接消息发送内部服务器通知给我自己和另一名工作人员通过API。我已经通过dev.twitter上的API设置,并下载了abraham's twitteroauth库。从twitter API发送直接消息

我需要直接从服务器发送消息(通过我的帐户),而无需每次都使用浏览器登录?上次我使用Twitter API是在oauth之前,所以很长一段时间过去了。

TL;博士需要通过Twitter发送私信,没有看到这个Twitter button

+0

您可以从[dev.twitter.com]你的应用页面(https://dev.twitter.com/apps)让您的个人令牌,并使用它,而无需'用Twitter登录'。 – maxdec

回答

16

您可以从您得到您的个人令牌应用页面上dev.twitter.com并使用它,而无需Sign in with Twitter

在您的应用页面的部分OAuth settings下得:

  • 消费者键
  • 消费者秘密

而下Your access token得到:

  • 访问令牌
  • 访问令牌机密

检查访问级别为Read, write, and direct messages。否则,在Settings选项卡中更改它并重新创建您的访问令牌(Details选项卡底部有一个按钮)。

然后在PHP

require('Abrahams library'); 

// Get everything you need from the dev.twitter.com/apps page 
$consumer_key = 'APP KEY'; 
$consumer_secret = 'APP SECRET'; 
$oauth_token = 'YOUR TOKEN'; 
$oauth_token_secret = 'YOUR TOKEN SECRET'; 

// Initialize the connection 
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret); 

// Send a direct message 
$options = array("screen_name" => "theGuyIWantToDM", "text" => "Hey that's my message"); 
$connection->post('direct_messages/new', $options); 
+0

谢谢,现在一切都完成了,代码工作得很好! –

+1

伟大的:-)这是圣诞节的魔力 – maxdec

相关问题