gmail oauth imap zend retrieve from gmail instead of session - zend-framework

Im planning to use google oauth IMAP to sign up for my website.
Im using zend framework for the same.
http://code.google.com/p/google-mail-xoauth-tools/wiki/PhpSampleCode
Also im suing 3 legged approach to sign up
When i go through the sample threelegged.php.
I find that i t has email address inbox and he keeps it in a session and goes to access the gmail account and once he returns back he retireves the email id from the session
$email_address = $_SESSION['email_address'];
Line No.121
$config = new Zend_Oauth_Config();
$config->setOptions($options);
$config->setToken($accessToken);
$config->setRequestMethod('GET');
$url = 'https://mail.google.com/mail/b/' .
$email_address .
'/imap/';
My requirement is i do not want to have email address to be kept in session instead i want the given the gmail address to be retrieved in
$email address.
How can i do that ?
Is any function supporting it in Zend framework?

http://sites.google.com/site/oauthgoog/Home/emaildisplayscope
You will need to include the authorization url with your other urls you are asking permission to get data from:
https://www.googleapis.com/auth/userinfo.email
Then when you actually get the authorized token you can do it like this:
// Get access token from session after authorization process is complete.
$accessToken = unserialize($_SESSION['ACCESS_TOKEN']);
// Retrieve email address using Access Token
$erequest = $accessToken->getHttpClient($options);
$erequest->setUri('https://www.googleapis.com/userinfo/email');
$erequest->setMethod(Zend_Http_Client::GET);
$ereturn = $erequest->request();
parse_str($ereturn->getBody(), $earray);
$email_address = $earray['email'];
// Retrieve mail using Access Token
$config = new Zend_Oauth_Config();
$config->setOptions($options);
$config->setToken($accessToken);
$config->setRequestMethod('GET');
$url = 'https://mail.google.com/mail/b/' .
$email_address .
'/imap/';
This is after authenticating a token for accessing the data. Then the email address can be used for your mail request or anything else that requires the email address.

Related

Check If access_token is valid

I have A Facebook App which auto post birthday reminders.
I stored tokens in database.
But Now most of them are expired.
I am trying to make a PHP file which will find valid tokens from my database and save them.
$query = mysql_query("select fb_token from fb_user")
while ($row = mysql_fetch_array($query)) {
$url = "https://graph.facebook.com/me?access_token=".$row['fb_token']."";
}
By with this Code I'm getting each token. Need Help to Save only valid tokens.
You can create request to the following Open Graph URL, basically you need to provide your APPID and access_token.
https://graph.facebook.com/oauth/access_token_info?client_id=APPID&access_token=xxxxxxxxx
The response will contain expiry time of specified token.

Send FB user a notification via App

I'm getting an App access token via the Facebook API.
$app_id = "ID";
$app_secret = "SECRET";
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=".$app_id."&client_secret=".$app_secret."&grant_type=client_credentials";
$app_token = file_get_contents($token_url);
When $app_token is echoed, the following is displayed
access_token=xxxxxxxxxxxxxxxxxxxxxxxx // access token x'ed out for security.
The "ID" is equal to my application ID (which I'm not silly enough to write). There's a vertical bar that separates the ID from another alphanumeric string.
I'm not very experienced with this aspect of the API, particularly doing what involves App access token rather than user access tokens.
So, when I tried to send a test notification in a php script
$url = "https://graph.facebook.com/".$fb_user_id."/notifications?href=".$url."&".$app_token;
$result = file_get_contents($url);
$result gives me an error message as such:
{"error":{"message":"A user access token is required to request this resource.","type":"OAuthException","code":102}}
As indicated on https://developers.facebook.com/docs/concepts/notifications/, you should using HTTP POST requests to send notification to the user of your apps, not HTTP GET requests.

"All users in param ids must have accepted TOS" Error

I am trying to get my Facebook app to send me a request or notification. When I do this with file_get_contents() as described here, I get a "Failed to open stream" error. The code I'm using is this:
//after getting the access token successfully...
$apprequest_url ="https://graph.facebook.com/".$user_id."/apprequests?message='Hi'&".$app_access_token.'&method=post';
$result = file_get_contents($apprequest_url);
When I try to do the same thing through the SDK, with this code:
$result = $facebook->api("/MY_USER_ID/apprequests", "POST", $param);
I get this error:
All users in param ids must have accepted TOS
However, I have authorized the app and it has used my e-mail address.Is there a specific permission I need to request to make this work?
It means the $user_id user doesn't use your app - you can only send app->user requests to existing users of your app

How can i login using email address in zend?

I have the situtation like , Admin should login as User from Admin End. I have user email address which is username for my site.
I am using following code in login page.
...
$users = new Default_Model_DbTable_Users();
$auth = Zend_Auth::getInstance();
$auth = Zend_Auth::getInstance();
$authAdapter = new Zend_Auth_Adapter_DbTable($users->getAdapter(),'customers');
$authAdapter->setIdentityColumn('email')->setCredentialColumn('password');
$authAdapter->setIdentity($username)->setCredential(base64_encode($password));
$authAdapter->getDbSelect()->where('status = 1');
$result = $auth->authenticate($authAdapter);
.....
Now I have to use only email address to login. I can check whether ADMIN do the user login from admin end. Is it possible to login using email address ?. Kindly advice on this
If you would like to allow admin to log in as any user from the admin panel, you don't have to use Zend_Auth::authenticate() to check any credentials against the database. All you really need to do is set up the identity like you do for a normal user login.
From admin you might do something like this:
$user = getUserInfoFromDatabase(); // get the user object used for Zend_Auth identity
$auth = Zend_Auth::getInstance()->getStorage()->write($user);
// redirect admin to user frontend
The only important thing is that whatever you write() to the storage, must be the EXACT same object/data you write to storage from your user login code.
It doesn't matter how the data gets there, as long as it is what your application requires to check identity.
Since your admin has already been authenticated and has permission to access a user account, you don't need to use Zend_Auth::authenticate() to validate the user email/password, you can skip that step and simply assign the identity for the admin directly to the session.
You may need to use separate session namespaces for admin and users if you are not already.
Hope that helps, let me know if I can clarify anything. The part where you set the identity is probably short after $result = $auth->authenticate($authAdapter); in your code, probably inside if ($result == true)

Having Trouble Accessing Email from Facebook Oauth

I can't get the proper token to reach the extended user information (specifically email) from the Facebook API. I've correctly given my app permission to retrieve the email, so I know I have the correct permissions. I'm trying to convert a session to a token using the exchange_session method as you can see below in my code:
$url = 'https://graph.facebook.com/oauth/exchange_sessions';
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$url);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS,'type=client_cred&client_id=' . FB_APPID . '&client_secret=' . FB_SECRET . '&sessions=' . $session->session_key);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
$tokendata = json_decode($buffer);
$user = json_decode(file_get_contents('https://graph.facebook.com/me/?wrap_access_token=' . $tokendata[0]->access_token));
print_r($user);
If I use the '/me' it always errors out with
"An active access token must be used to query information about the current user."
If I swap out '/me' with the current user ID, it will give me the basic user info, but not the extended info.
The token that I get back is indeed the full token with session info in this format:
123456789012345|6.abcdefghijklmnopqrstuv__.1234.1234567890-1234567890|abcdefghijklmnopqrstuvwxyza.
Help?
Thanks!
Andy
Should it be /me/?access_token= instead of /me/?wrap_access_token=?
Use official facebook's php sdk to avoid the headache with tokens handling