Figuring out shared authorships, particularly when you are a graduate student in a hierarchical relationship with the other author(s), can be challenging. My advice is to have the conversation early on and in no uncertain terms. While this conversation can be uncomfortable, it minimizes tensions, frustrations, and passive-aggressiveness at the outset.
If you are conducting research with or for a professor ask him/her what you can expect out of this research experience. If second or first authorship is possible (for yourself) ask exactly what is required of you to obtain it (data analysis, literature review, writing, etc).
In the converse, if you would like to use a professor's data set and don't want to tag him/her as a second author - make that clear upfront. I was planning on adding a comparative component to my dissertation using a data set I had been a research assistant on in my first few years in graduate school. One of my advisors told me that it was imperative that I speak with the principle investigators of the data set and insure that I could “acknowledge” them in the articles/book that might come out of it, rather than name them as supporting authors. They agreed to this and it took a lot of worry and ambiguity out of the situation.
Finally, even if you have had this conversation you may need to restate the "terms of your agreement" at certain points throughout the collaborative experience. The professor may make a statement about his/her role that is different from what you originally agreed on (I think in most cases this is just the product of absentmindedness due to an overloaded work schedule) and you will need to reference your original conversation. But, if you haven't talked things through clearly in the beginning you may find yourself giving up things that are very near and dear to any graduate student (single or first authorship)!